Dipendenze reali con Testcontainers
Esegua test su database reali in container.
Dipendenze reali con Testcontainers è una lezione Spring Boot 4 Microservices & REST APIs gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Boot 4 Microservices & REST APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Domande Frequenti
La lezione «Dipendenze reali con Testcontainers» è gratuita?
Sì — il testo completo di «Dipendenze reali con Testcontainers» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Boot 4 Microservices & REST APIs, passa a CoddyKit PRO. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Cosa imparerò in «Dipendenze reali con Testcontainers»?
Esegua test su database reali in container. Eserciti Spring Boot 4 Microservices & REST APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Spring Boot 4 Microservices & REST APIs?
Non è richiesta alcuna esperienza precedente. Spring Boot 4 Microservices & REST APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Dipendenze reali con Testcontainers»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Spring Boot 4 Microservices & REST APIs?
Sì. Ogni lezione Spring Boot 4 Microservices & REST APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Test unitari con JUnit e Mockito
- Test del web layer con MockMvc
- Test di integrazione con @SpringBootTest
- Dipendenze reali con Testcontainers