Spring Boot testing
30 questions in 10 subtopics.
@SpringBootTest
Explain thewebEnvironment options of @SpringBootTest (MOCK, RANDOM_PORT, DEFINED_PORT, NONE) and when you would use each when testing a Spring MVC service.A team's @SpringBootTest suite fails on CI with 'Failed to load ApplicationContext' because a bean tries to connect to an external payment API at startup. How would you make the tests independent of that API?Your service has 600 tests annotated with @SpringBootTest and the build takes 25 minutes. How would you reduce the time without losing confidence?Across 50 Spring Boot services, test practices vary widely: some teams only have @SpringBootTest end-to-end tests, others only mocks. As the architect, what test strategy standard would you define?Engineering spends a large and growing share of its CI budget and developer waiting time on test suites, while escaped defects are not falling. As the senior technical leader, how do you set direction on test strategy and its cost?Leadership asks whether to keep funding a large shared end-to-end test environment that is slow, often broken and blocks releases, or invest in contract and service-level tests. As the senior technical leader, how do you make and communicate this decision?Test slices
What does@WebMvcTest load and not load, and how does it differ from @DataJpaTest?Your team wants a custom test slice for Kafka listeners that loads only the listener beans and Kafka infrastructure. How are Spring Boot slices built, and would you build one?MockMvc
Write aMockMvc test for a POST /orders endpoint that checks a 201 status, a Location header and a JSON field, and another test for validation returning 400.A MockMvc test passes but the same request fails in production with a 500 because of a response encoding or error page issue. Why can MockMvc and a real server behave differently?WebTestClient
How would you test a Spring WebFlux endpoint that returns a Flux of server-sent events withWebTestClient?Your team must choose between WebTestClient, TestRestTemplate and RestTestClient for HTTP integration tests of a Spring MVC service on Spring Boot 4. What would you consider?@MockitoBean
Spring Boot 3.4 deprecated@MockBean in favour of @MockitoBean. What does @MockitoBean do, where does it come from, and what should you watch for when migrating?After adding @MockitoBean for a repository in one test class, the test suite became much slower and a different test class started failing because a mock leaked. Explain both effects.Context caching
How does the SpringTestContext framework cache application contexts between test classes, and what makes two test classes share a context?Your CI runs out of memory and database connections during tests. You find 40 cached application contexts, each with its own Hikari pool and Kafka consumers. What would you do?Across 40 Spring Boot services, CI time is dominated by integration tests: each build creates 20 to 60 application contexts and starts its own containers. As the architect, how would you reduce test-suite cost across teams without lowering confidence?Testcontainers with @ServiceConnection
Show how to use a PostgreSQL Testcontainers container with@ServiceConnection in a Spring Boot test, and explain what @ServiceConnection replaces.Your developers want to run the application locally with the same containers used in tests. How can Spring Boot's Testcontainers support do this, and how would you keep startup fast in CI?Define a company standard for integration testing of Spring Boot services against real infrastructure (databases, Kafka, Redis) that works for 60 teams on shared CI runners.Testing security
How would you test that GET /admin/users returns 403 for a normal user and 200 for an admin, and that a POST requires a CSRF token, in a Spring Security protected MVC app?Your resource server validates JWTs and maps a 'scope' claim to authorities. How would you test authorization rules without a real identity provider?A security review found that several services had authorization regressions that tests did not catch. How would you raise security test standards across teams?After a regulator's audit, your company must show evidence that authorization rules in all customer-facing services are tested. As the senior technical leader, how do you turn this into a lasting engineering standard rather than a one-off exercise?Testing transactions
In a Spring test annotated with@Transactional, data inserted in the test is rolled back at the end. Explain how this works, and why a test using RANDOM_PORT and an HTTP client does not see this rollback.A @DataJpaTest passes, but in production the same code fails with a LazyInitializationException and a constraint violation that tests never saw. Why might transactional tests hide these problems?Your company's core systems rely on transactional correctness, but test practices differ across teams and many bugs reach production. As the senior technical leader, how would you set direction on test investment for data correctness?@TestConfiguration and test beans
What is the difference between@TestConfiguration and @Configuration in Spring Boot tests? When is a @TestConfiguration class picked up automatically, and when must you @Import it?A test adds a @TestConfiguration that defines a Clock bean to replace the application's Clock, but startup fails with BeanDefinitionOverrideException. Explain why and give three ways to replace the bean correctly.Across many services, tests define dozens of slightly different @TestConfiguration classes and overrides, so each build creates many application contexts and test behaviour differs from production wiring. Define guidance and shared tooling to fix this.No question matches these choices.