0Pricing
Spring Boot 4 Microservices & REST APIs · Lekcja

Testy integracyjne z @SpringBootTest

Testuj cały kontekst aplikacji

Testy integracyjne z @SpringBootTest to bezpłatna lekcja Spring Boot 4 Microservices & REST APIs na CoddyKit. To lekcja 3 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.

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 environment
  • RANDOM_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:

  • @SpringBootTest loads the full context
  • RANDOM_PORT + TestRestTemplate/WebTestClient for real HTTP
  • @Transactional rolls back DB changes
  • Use slices for speed

Często zadawane pytania

Czy lekcja „Testy integracyjne z @SpringBootTest” jest bezpłatna?

Tak — pełny tekst „Testy integracyjne z @SpringBootTest” 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 „Testy integracyjne z @SpringBootTest”?

Testuj cały kontekst aplikacji Ć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 3 z 4.

Ile czasu zajmuje lekcja „Testy integracyjne z @SpringBootTest”?

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