Jānis Ošs’ Post

🟢 Spring Boot: Integration tests ar @SpringBootTest 🧪 Integration Testing in Spring Boot with @SpringBootTest Unit tests verify isolated pieces of logic, but real bugs often hide in the seams — where controllers talk to services, services talk to repositories, and repositories talk to a database. That's where @SpringBootTest shines. @SpringBootTest boots the full Spring application context (or a slice of it) so your tests run against real beans, real configuration, and — with Testcontainers — a real database. No mocking your own code. No fragile assumptions. Key practices I rely on: ▪️ Use webEnvironment = RANDOM_PORT with TestRestTemplate or WebTestClient to hit actual HTTP endpoints ▪️ Combine with @Testcontainers + PostgreSQLContainer instead of H2 — test against the same DB engine you run in production ▪️ Use @DynamicPropertySource to inject container JDBC URLs at runtime ▪️ Reuse the Spring context across tests (avoid @DirtiesContext unless necessary) — startup is the biggest cost ▪️ Seed data with @Sql scripts or test fixtures, and isolate tests with @Transactional rollback where appropriate ▪️ Layer your tests: @WebMvcTest for the web slice, @DataJpaTest for JPA slice, @SpringBootTest for full end-to-end flows Integration tests are slower than unit tests — that's the trade-off. But the confidence they give when refactoring or upgrading Spring Boot versions is worth every extra second in CI. #SpringBoot #Java #IntegrationTesting #Testcontainers #SoftwareEngineering #Backend #TestAutomation #JUnit

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories