Spring Boot Testing Spring Applications 4 — Questions and Answers
Question 1: What does the @DirtiesContext annotation signal to the Spring test framework?
- The test uses a dirty read database isolation level
- The Spring application context should be closed and rebuilt after the test (Correct answer)
- Mock beans should be reset between test methods
- The test is expected to throw an exception
Correct answer: The Spring application context should be closed and rebuilt after the test
@DirtiesContext tells the framework that the test modifies the shared Spring context, so it must be discarded and recreated for subsequent tests.
Question 2: Which assertion library, included in spring-boot-starter-test, uses a fluent 'assertThat' API?
- Hamcrest
- AssertJ (Correct answer)
- Truth
- JUnit Assertions
Correct answer: AssertJ
AssertJ provides a fluent assertThat() API with rich, readable assertions and is bundled by default with Spring Boot's test starter.
Question 3: When using TestRestTemplate, how do you make a POST request and capture the response body as a custom object?
- restTemplate.post(url, request)
- restTemplate.postForObject(url, request, MyClass.class)
- restTemplate.exchange(url, HttpMethod.POST, request, MyClass.class)
- Both B and C are correct (Correct answer)
Correct answer: Both B and C are correct
Both postForObject() and exchange() can deserialize the response body into a custom class; exchange() additionally gives access to headers and status.
Question 4: Which annotation allows you to override specific auto-configuration classes in a Spring Boot test?
- @TestConfiguration
- @OverrideAutoConfiguration
- @ExcludeAutoConfiguration
- @SpringBootTest(exclude={...}) (Correct answer)
Correct answer: @SpringBootTest(exclude={...})
@SpringBootTest(exclude={SomeAutoConfig.class}) prevents the specified auto-configuration classes from being applied during the test.
Question 5: What is the role of @TestConfiguration in Spring Boot tests?
- Replaces @SpringBootTest for lightweight tests
- Defines additional beans to add or override in the test context (Correct answer)
- Configures test database connection properties
- Enables test-profile-specific application.properties
Correct answer: Defines additional beans to add or override in the test context
@TestConfiguration marks a class that provides beans added to or replacing existing beans in the test application context without affecting the production context.
Question 6: When testing a @RestController with @WebMvcTest, how do you mock a service dependency?
- Annotate the service field with @Mock
- Use @MockBean in the test class
- Override the bean in a @TestConfiguration class
- Both B and C work (Correct answer)
Correct answer: Both B and C work
Both @MockBean and a @TestConfiguration with an @Bean definition can inject a mock service into the limited context loaded by @WebMvcTest.
Question 7: What does jsonPath('$.name').value('Alice') verify in a MockMvc test?
- That the request body contains a field name with value Alice
- That the JSON response body has a top-level field 'name' equal to 'Alice' (Correct answer)
- That the JSON schema defines a required 'name' field
- That the Alice bean is registered in the Spring context
Correct answer: That the JSON response body has a top-level field 'name' equal to 'Alice'
jsonPath() evaluates a JSONPath expression against the response body, and .value() asserts the matched node equals the expected value.
What does the @DirtiesContext annotation signal to the Spring test framework?