0Pricing
Spring Boot 4 Microservices & REST APIs · Lekcja

Rzeczywiste zależności z Testcontainers

Testuj aplikację na rzeczywistych bazach danych w kontenerach

Rzeczywiste zależności z Testcontainers to bezpłatna lekcja Spring Boot 4 Microservices & REST APIs na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Boot 4 Microservices & REST APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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 + @Container start Docker services
  • PostgreSQLContainer for a real database
  • @DynamicPropertySource or @ServiceConnection wires the datasource
  • Requires a running Docker engine

Często zadawane pytania

Czy lekcja „Rzeczywiste zależności z Testcontainers” jest bezpłatna?

Tak — pełny tekst „Rzeczywiste zależności z Testcontainers” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Boot 4 Microservices & REST APIs, przejdź na CoddyKit PRO. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.

Co nauczysz się w „Rzeczywiste zależności z Testcontainers”?

Testuj aplikację na rzeczywistych bazach danych w kontenerach Ćwiczysz Spring Boot 4 Microservices & REST APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Spring Boot 4 Microservices & REST APIs?

Nie wymagamy żadnego doświadczenia. Spring Boot 4 Microservices & REST APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Rzeczywiste zależności z Testcontainers”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Spring Boot 4 Microservices & REST APIs?

Tak. Każda lekcja Spring Boot 4 Microservices & REST APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Testy jednostkowe z JUnit i Mockito
  2. Testy warstwy Web z MockMvc
  3. Testy integracyjne z @SpringBootTest
  4. Rzeczywiste zależności z Testcontainers
← Powrót do Spring Boot 4 Microservices & REST APIs