Dependensi Nyata dengan Testcontainers
Uji aplikasi menggunakan basis data nyata di dalam kontainer.
Dependensi Nyata dengan Testcontainers adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Boot 4 Microservices & REST APIs, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Dependensi Nyata dengan Testcontainers” gratis?
Ya — teks lengkap “Dependensi Nyata dengan Testcontainers” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Boot 4 Microservices & REST APIs, upgrade ke CoddyKit PRO. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Dependensi Nyata dengan Testcontainers”?
Uji aplikasi menggunakan basis data nyata di dalam kontainer. Kamu berlatih Spring Boot 4 Microservices & REST APIs dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Spring Boot 4 Microservices & REST APIs?
Tidak diperlukan pengalaman sebelumnya. Spring Boot 4 Microservices & REST APIs di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Dependensi Nyata dengan Testcontainers” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Boot 4 Microservices & REST APIs ini?
Ya. Setiap pelajaran Spring Boot 4 Microservices & REST APIs menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pengujian Unit dengan JUnit dan Mockito
- Pengujian Lapisan Web dengan MockMvc
- Pengujian Integrasi dengan @SpringBootTest
- Dependensi Nyata dengan Testcontainers