Spring Boot มอบ testing pyramid ให้คุณ: unit test ที่รวดเร็วโดยไม่ต้องใช้ Spring, slice test ที่โฟกัสและโหลด context เพียงบางส่วน และ integration test แบบ full-context เพียงไม่กี่ตัว การเลือกระดับที่เหมาะสมต่อเทสต์แต่ละตัวคือทักษะระดับ senior ในเรื่องนี้
Unit test — ไม่ใช้ Spring เลย
constructor injection หมายความว่าตรรกะของ service ส่วนใหญ่ไม่ต้องใช้ Spring เลย เพียง mock collaborator ก็พอ:
{
mock(PriceRepository.class);
(repo.base()).thenReturn(BigDecimal.TEN);
(repo);
assertThat(svc.priceOf()).isEqualByComparingTo();
}
