ส่วนพึ่งพาจริงด้วย Testcontainers
ทดสอบกับฐานข้อมูลจริงในคอนเทนเนอร์
ส่วนพึ่งพาจริงด้วย Testcontainers เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Testcontainers
Testcontainers runs real services — PostgreSQL, Redis, Kafka — inside Docker containers during tests.
You test against the real database, not an in-memory substitute, catching dialect and behavior differences.
Adding the Dependency
Add the Testcontainers JUnit and database modules to your build.
// build.gradle
testImplementation "org.testcontainers:junit-jupiter"
testImplementation "org.testcontainers:postgresql"The @Testcontainers Annotation
@Testcontainers on the test class activates container lifecycle management.
@Testcontainers
@SpringBootTest
class UserRepositoryIT {
// containers declared here
}Declaring a PostgreSQLContainer
A static @Container field defines the database image to start.
@Container
static PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16")
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");Wiring the Datasource
Use @DynamicPropertySource to inject the container's generated JDBC URL into Spring.
@DynamicPropertySource
static void props(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url",
postgres::getJdbcUrl);
registry.add("spring.datasource.username",
postgres::getUsername);
registry.add("spring.datasource.password",
postgres::getPassword);
}A Full Repository Test
Now repository tests run against real PostgreSQL.
@Test
void savesAndReads() {
User saved = repository.save(new User("Alice"));
assertThat(repository.findById(saved.getId()))
.isPresent();
}Static vs Instance Containers
A static container starts once for all tests in the class (faster). A non-static container restarts for each test (more isolation).
Other Container Types
Testcontainers has modules for many services.
@Container
static GenericContainer<?> redis =
new GenericContainer<>("redis:7")
.withExposedPorts(6379);Reusing Containers
Enable container reuse to speed up local runs across test executions.
@Container
static PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16")
.withReuse(true);Spring Boot Service Connections
Spring Boot 3.1+ can auto-configure the datasource from a container with @ServiceConnection, removing the need for @DynamicPropertySource.
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16");Docker Requirement
Testcontainers needs a running Docker engine on the machine or CI agent. Without Docker the tests fail to start containers.
Quick Check
Test your understanding of Testcontainers.
Recap
You learned to test against real dependencies:
@Testcontainers+@Containerstart Docker servicesPostgreSQLContainerfor a real database@DynamicPropertySourceor@ServiceConnectionwires the datasource- Requires a running Docker engine
คำถามที่พบบ่อย
บทเรียน “ส่วนพึ่งพาจริงด้วย Testcontainers” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ส่วนพึ่งพาจริงด้วย Testcontainers” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ส่วนพึ่งพาจริงด้วย Testcontainers”
ทดสอบกับฐานข้อมูลจริงในคอนเทนเนอร์ คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ส่วนพึ่งพาจริงด้วย Testcontainers” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทดสอบหน่วยด้วย JUnit และ Mockito
- การทดสอบชั้นเว็บด้วย MockMvc
- การทดสอบการผสานรวมด้วย @SpringBootTest
- ส่วนพึ่งพาจริงด้วย Testcontainers