Spring Boot Testing Spring Applications 3 — Questions and Answers
Question 1: Which tool is the default test framework bundled with Spring Boot starter test?
- TestNG
- Spock
- JUnit 5 (Jupiter) (Correct answer)
- JUnit 4
Correct answer: JUnit 5 (Jupiter)
spring-boot-starter-test includes JUnit 5 (Jupiter) as the default testing framework since Spring Boot 2.2.
Question 2: What does @ExtendWith(SpringExtension.class) do in a JUnit 5 Spring test?
- Enables Mockito annotations only
- Integrates the Spring TestContext Framework with JUnit 5 (Correct answer)
- Loads the full Spring Boot auto-configuration
- Replaces @RunWith from JUnit 4
Correct answer: Integrates the Spring TestContext Framework with JUnit 5
@ExtendWith(SpringExtension.class) hooks the Spring TestContext Framework lifecycle into JUnit 5, enabling Spring context loading and dependency injection in tests.
Question 3: Which HTTP client is recommended for integration tests when using a real embedded server with WebFlux?
- MockMvc
- TestRestTemplate
- WebTestClient (Correct answer)
- RestTemplate
Correct answer: WebTestClient
WebTestClient provides a reactive, fluent API suitable for testing both WebFlux and Servlet-based endpoints against a running server.
Question 4: What annotation should you use to test only the Spring Security configuration layer without a full context?
- @SpringBootTest
- @WebMvcTest with @WithMockUser (Correct answer)
- @SecurityTest
- @AutoConfigureSecurity
Correct answer: @WebMvcTest with @WithMockUser
@WebMvcTest loads only the MVC layer and @WithMockUser provides a mock authenticated user for security testing.
Question 5: What is the effect of annotating a test class with @Transactional in Spring tests?
- Commits every database write during the test
- Rolls back the transaction after each test method by default (Correct answer)
- Disables JPA entity caching
- Enables distributed transactions across data sources
Correct answer: Rolls back the transaction after each test method by default
In Spring test classes, @Transactional causes each test method to run in a transaction that is automatically rolled back after the method completes.
Question 6: Which Spring Boot test slice annotation would you use to test a @Repository that uses Spring Data JDBC?
- @WebMvcTest
- @DataJdbcTest (Correct answer)
- @DataJpaTest
- @SpringBootTest
Correct answer: @DataJdbcTest
@DataJdbcTest configures only Spring Data JDBC components and an embedded database, making it the correct slice for testing JDBC repositories.
Question 7: How does @SpyBean differ from @MockBean in Spring tests?
- @SpyBean wraps a real bean and allows partial mocking; @MockBean replaces it with a full mock (Correct answer)
- @SpyBean creates a new mock with no real logic; @MockBean wraps a real bean
- @SpyBean only works with interfaces; @MockBean works with classes
- @SpyBean is for controllers; @MockBean is for services
Correct answer: @SpyBean wraps a real bean and allows partial mocking; @MockBean replaces it with a full mock
@SpyBean wraps the actual Spring bean with a Mockito spy, so real methods are called unless explicitly stubbed, while @MockBean replaces the bean with a full Mockito mock.
Which tool is the default test framework bundled with Spring Boot starter test?