การทดสอบการผสานรวมด้วย @SpringBootTest
ทดสอบบริบทของแอปทั้งหมด
การทดสอบการผสานรวมด้วย @SpringBootTest เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is an Integration Test
An integration test exercises multiple layers together — controller, service, repository — using a real Spring context.
It catches wiring and configuration problems that unit tests miss.
@SpringBootTest
@SpringBootTest boots the entire application context, loading all beans.
@SpringBootTest
class ApplicationIntegrationTest {
@Autowired
UserService userService;
@Test
void contextLoads() {
assertThat(userService).isNotNull();
}
}WebEnvironment Modes
Control whether a real server starts with the webEnvironment attribute.
MOCK(default) — mock servlet environmentRANDOM_PORT— real server on a random port
@SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT)
class ApiTest { }Testing with TestRestTemplate
With a real port you can make actual HTTP calls using TestRestTemplate.
@Autowired
TestRestTemplate restTemplate;
@Test
void getsUser() {
ResponseEntity<User> response =
restTemplate.getForEntity("/users/1", User.class);
assertThat(response.getStatusCode())
.isEqualTo(HttpStatus.OK);
}WebTestClient
WebTestClient is a fluent client that works for both MVC and WebFlux apps.
@Autowired
WebTestClient webTestClient;
@Test
void getsUser() {
webTestClient.get().uri("/users/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("Alice");
}Using a Test Database
By default Spring Boot can replace your datasource with an in-memory H2 database for tests.
@SpringBootTest
@AutoConfigureTestDatabase
class RepositoryIntegrationTest {
@Autowired UserRepository repository;
}Test Properties
Override configuration for tests with @TestPropertySource or an application-test.properties profile.
@SpringBootTest
@TestPropertySource(properties = {
"feature.enabled=false"
})
class FeatureTest { }Activating Profiles
Use @ActiveProfiles to run with a specific Spring profile.
@SpringBootTest
@ActiveProfiles("test")
class ServiceTest { }Transactional Rollback
Annotate tests with @Transactional so each test's database changes roll back automatically, keeping tests isolated.
@SpringBootTest
@Transactional
class UserPersistenceTest {
@Test
void savesAndRollsBack() {
repository.save(new User("Alice"));
}
}Slicing for Speed
Full @SpringBootTest is slow. Prefer slices like @DataJpaTest or @WebMvcTest when you only need one layer.
@DataJpaTest
@DataJpaTest loads only JPA components and an embedded database — ideal for repository tests.
@DataJpaTest
class UserRepositoryTest {
@Autowired UserRepository repository;
@Test
void findsByName() {
repository.save(new User("Alice"));
assertThat(repository.findByName("Alice"))
.hasSize(1);
}
}Quick Check
Test your understanding of integration testing.
Recap
You learned to write integration tests:
@SpringBootTestloads the full contextRANDOM_PORT+TestRestTemplate/WebTestClientfor real HTTP@Transactionalrolls back DB changes- Use slices for speed
คำถามที่พบบ่อย
บทเรียน “การทดสอบการผสานรวมด้วย @SpringBootTest” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบการผสานรวมด้วย @SpringBootTest” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบการผสานรวมด้วย @SpringBootTest”
ทดสอบบริบทของแอปทั้งหมด คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบการผสานรวมด้วย @SpringBootTest” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทดสอบหน่วยด้วย JUnit และ Mockito
- การทดสอบชั้นเว็บด้วย MockMvc
- การทดสอบการผสานรวมด้วย @SpringBootTest
- ส่วนพึ่งพาจริงด้วย Testcontainers