Spring Framework Quality Control & Assurance 2 — Questions and Answers
Question 1: Which Spring Test annotation loads only a subset of the application context for testing the web layer without starting a full server?
- @SpringBootTest
- @WebMvcTest (Correct answer)
- @DataJpaTest
- @ContextConfiguration
Correct answer: @WebMvcTest
@WebMvcTest loads only MVC components like controllers, filters, and WebMvcConfigurer beans, not the full context.
Question 2: In Spring Boot, what does the @DataJpaTest annotation do by default regarding the datasource?
- Uses the production datasource defined in application.properties
- Configures an in-memory embedded database (Correct answer)
- Disables all datasource configuration
- Loads only repository beans without a database
Correct answer: Configures an in-memory embedded database
@DataJpaTest replaces the configured datasource with an embedded in-memory database by default for isolated repository testing.
Question 3: Which assertion library is commonly used with Spring Boot Test to provide more readable assertions like assertThat(value).isEqualTo(expected)?
- Hamcrest
- AssertJ (Correct answer)
- TestNG
- JUnit Assert
Correct answer: AssertJ
AssertJ provides a fluent API with assertThat() that Spring Boot Test auto-imports via spring-boot-starter-test.
Question 4: What is the purpose of @MockBean in a Spring Boot test?
- Creates a real Spring bean backed by a mock object and adds it to the ApplicationContext (Correct answer)
- Creates a Mockito mock without registering it in the Spring context
- Replaces the entire ApplicationContext with mocks
- Mocks only repository-layer beans
Correct answer: Creates a real Spring bean backed by a mock object and adds it to the ApplicationContext
@MockBean creates a Mockito mock and registers it as a Spring bean, replacing any existing bean of the same type in the context.
Question 5: When using MockMvc, which method is used to simulate an HTTP GET request to a controller endpoint?
- mockMvc.perform(get("/path")) (Correct answer)
- mockMvc.request(HttpMethod.GET, "/path")
- mockMvc.simulate(MockMvcRequestBuilders.get("/path"))
- mockMvc.invoke(get("/path"))
Correct answer: mockMvc.perform(get("/path"))
MockMvc.perform() accepts a RequestBuilder such as MockMvcRequestBuilders.get() to simulate HTTP requests in tests.
Question 6: Which Spring annotation can be applied to a test class to replace beans with Spy objects that wrap real implementations?
- @MockBean
- @SpyBean (Correct answer)
- @FakeBean
- @WrapBean
Correct answer: @SpyBean
@SpyBean wraps an existing Spring bean with a Mockito spy, allowing partial mocking while retaining real method behavior.
Question 7: In a Spring Boot integration test, what does setting webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT do?
- Starts an embedded server on a randomly chosen available port (Correct answer)
- Disables the web environment entirely
- Uses MockMvc instead of a real server
- Sets the port to 0 permanently in application.properties
Correct answer: Starts an embedded server on a randomly chosen available port
RANDOM_PORT starts a real embedded web server on a random available port, preventing port conflicts during parallel test execution.
Which Spring Test annotation loads only a subset of the application context for testing the web layer without starting a full server?