Scrivio Interview library

Spring Boot testing

30 questions in 10 subtopics.

@SpringBootTest

ConceptFoundations5 min read
Explain the webEnvironment options of @SpringBootTest (MOCK, RANDOM_PORT, DEFINED_PORT, NONE) and when you would use each when testing a Spring MVC service.
DebuggingFoundations6 min read
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?
ScenarioIntermediate5 min read
Your service has 600 tests annotated with @SpringBootTest and the build takes 25 minutes. How would you reduce the time without losing confidence?
LeadershipAdvanced6 min read
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?
LeadershipPrincipal6 min read
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?
LeadershipPrincipal6 min read
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

ConceptFoundations5 min read
What does @WebMvcTest load and not load, and how does it differ from @DataJpaTest?
DesignIntermediate6 min read
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

CodingFoundations5 min read
Write a MockMvc 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.
DebuggingIntermediate5 min read
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

CodingFoundations5 min read
How would you test a Spring WebFlux endpoint that returns a Flux of server-sent events with WebTestClient?
ScenarioIntermediate5 min read
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

ConceptFoundations5 min read
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?
DebuggingIntermediate5 min read
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

InternalsFoundations5 min read
How does the Spring TestContext framework cache application contexts between test classes, and what makes two test classes share a context?
ScenarioIntermediate5 min read
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?
ScenarioAdvanced5 min read
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

CodingFoundations5 min read
Show how to use a PostgreSQL Testcontainers container with @ServiceConnection in a Spring Boot test, and explain what @ServiceConnection replaces.
DesignIntermediate5 min read
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?
DesignAdvanced6 min read
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

CodingFoundations5 min read
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?
CodingIntermediate7 min read
Your resource server validates JWTs and maps a 'scope' claim to authorities. How would you test authorization rules without a real identity provider?
LeadershipAdvanced6 min read
A security review found that several services had authorization regressions that tests did not catch. How would you raise security test standards across teams?
LeadershipPrincipal6 min read
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

ConceptFoundations7 min read
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.
DebuggingIntermediate7 min read
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?
LeadershipPrincipal6 min read
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

ConceptFoundations6 min read
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?
DebuggingIntermediate7 min read
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.
DesignAdvanced6 min read
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.