Spring Framework Quality Control & Assurance 5 — Questions and Answers
Question 1: Which Spring Boot feature allows you to override specific properties only during tests without modifying application.properties?
- @TestPropertySource or properties attribute in @SpringBootTest (Correct answer)
- @PropertyOverride annotation
- Modifying Environment bean in @BeforeAll
- Using @Value with default expressions
Correct answer: @TestPropertySource or properties attribute in @SpringBootTest
@TestPropertySource or the properties/args attribute on @SpringBootTest injects test-specific property values that override the main configuration.
Question 2: What is the purpose of the spring.test.mockmvc.print property or .andDo(print()) in MockMvc tests?
- Outputs the test result to a PDF report
- Prints the full HTTP request and response details to the console for debugging (Correct answer)
- Logs only failed assertions to System.err
- Generates an HTML test report
Correct answer: Prints the full HTTP request and response details to the console for debugging
.andDo(print()) outputs the complete request/response details including headers, body, and status to the console during test execution.
Question 3: In Spring Boot, which annotation enables support for Spring Security in @WebMvcTest slices?
- @EnableSecurity
- @WithMockUser is sufficient without any additional annotation (Correct answer)
- @ImportSecurity
- @SecurityTest
Correct answer: @WithMockUser is sufficient without any additional annotation
@WithMockUser from the Spring Security Test module sets up a mock authenticated user for controller tests without additional configuration.
Question 4: What does code coverage measurement in a Spring Boot project typically analyze?
- The number of Spring beans created during tests
- The percentage of production code lines or branches executed by the test suite (Correct answer)
- The number of HTTP endpoints covered by integration tests
- The ratio of unit tests to integration tests
Correct answer: The percentage of production code lines or branches executed by the test suite
Code coverage tools like JaCoCo measure what percentage of source code lines and branches were executed during the test suite run.
Question 5: Which Spring Cloud Contract feature helps ensure API contracts are met between microservices?
- Generates producer-side tests from consumer-defined contracts and stubs for consumer tests (Correct answer)
- Monitors API calls in production and alerts on deviations
- Automatically generates OpenAPI specs from Spring MVC annotations
- Replaces integration tests with static analysis
Correct answer: Generates producer-side tests from consumer-defined contracts and stubs for consumer tests
Spring Cloud Contract generates tests for the producer side and stubs for consumers based on shared contract definitions, preventing breaking API changes.
Question 6: Which metric exposed by Spring Boot Actuator is most useful for detecting memory leaks in a running application?
- jvm.gc.pause
- jvm.memory.used (Correct answer)
- system.cpu.usage
- process.uptime
Correct answer: jvm.memory.used
jvm.memory.used tracks heap and non-heap memory consumption over time; a consistently rising trend indicates a potential memory leak.
Question 7: In a Spring Boot test, what is the recommended way to test exception handling in a REST controller using MockMvc?
- Wrap the perform() call in a try-catch block
- Use .andExpect(status().is4xxClientError()) or specific status matchers after triggering the error condition (Correct answer)
- Annotate the test with @ExpectedException
- Use assertThrows() from JUnit 5 around the controller method call
Correct answer: Use .andExpect(status().is4xxClientError()) or specific status matchers after triggering the error condition
MockMvc's result matchers like status().isBadRequest() or status().isInternalServerError() assert the HTTP error response returned by exception handlers.
Which Spring Boot feature allows you to override specific properties only during tests without modifying application.properties?