Spring Framework Quality Control & Assurance 4 — Questions and Answers
Question 1: Which annotation in Spring Boot Test is used to auto-configure a TestRestTemplate for full integration tests?
- @WebMvcTest
- @SpringBootTest with webEnvironment = RANDOM_PORT (Correct answer)
- @RestClientTest
- @DataJpaTest
Correct answer: @SpringBootTest with webEnvironment = RANDOM_PORT
When @SpringBootTest uses RANDOM_PORT or DEFINED_PORT, Spring Boot auto-configures a TestRestTemplate bean for HTTP client testing.
Question 2: In Spring's testing support, what does ApplicationContext caching mean?
- Test results are cached to avoid re-running slow tests
- The ApplicationContext is reused across tests with the same configuration, reducing startup time (Correct answer)
- Bean definitions are cached on disk between CI runs
- Mockito mocks are cached between test classes
Correct answer: The ApplicationContext is reused across tests with the same configuration, reducing startup time
Spring's TestContext Framework caches the ApplicationContext so tests sharing the same configuration reuse the same context, speeding up test suites.
Question 3: What is the effect of @DirtiesContext on the Spring test ApplicationContext?
- Clears the context cache so a fresh context is created for subsequent tests (Correct answer)
- Marks the context as read-only for that test
- Reloads only the modified beans without restarting the full context
- Disables bean post-processors for that test class
Correct answer: Clears the context cache so a fresh context is created for subsequent tests
@DirtiesContext forces Spring to close and recreate the ApplicationContext after the annotated test, useful when a test modifies shared state.
Question 4: Which Spring Boot testing slice would you use to test a REST client that calls an external API, with the HTTP layer mocked?
- @WebMvcTest
- @DataJpaTest
- @RestClientTest (Correct answer)
- @SpringBootTest
Correct answer: @RestClientTest
@RestClientTest configures a MockRestServiceServer to intercept and verify HTTP calls made by RestTemplate or RestClient beans.
Question 5: In Spring MVC tests, what does .andExpect(status().isOk()) verify?
- The response body is not empty
- The HTTP response status code is 200 (Correct answer)
- The controller method completed without throwing an exception
- The Content-Type header is application/json
Correct answer: The HTTP response status code is 200
status().isOk() is a ResultMatcher that asserts the HTTP response status is 200 OK.
Question 6: What is the purpose of WireMock when used in Spring Boot integration tests?
- Mocks Spring beans at the ApplicationContext level
- Stubs and verifies HTTP interactions with external services (Correct answer)
- Generates mock data for database tests
- Replaces Spring Security in test environments
Correct answer: Stubs and verifies HTTP interactions with external services
WireMock runs a real HTTP server that stubs responses and verifies request interactions, enabling tests of HTTP client code against realistic endpoints.
Question 7: Which Mockito method call pattern is used to stub a method on a @MockBean to return a specific value?
- Mockito.stub(bean.method()).toReturn(value)
- when(bean.method()).thenReturn(value) (Correct answer)
- bean.method().returns(value)
- doReturn(value).when(bean, "method")
Correct answer: when(bean.method()).thenReturn(value)
Mockito's when().thenReturn() pattern stubs a method call on a mock to return a predefined value when invoked.
Which annotation in Spring Boot Test is used to auto-configure a TestRestTemplate for full integration tests?