Spring Boot Testing
Spring Boot testing: @SpringBootTest, @WebMvcTest, @DataJpaTest, MockMvc, TestRestTemplate, test slices
1Which annotation to use to load the full Spring Boot context for an integration test?
Which annotation to use to load the full Spring Boot context for an integration test?
답변
@SpringBootTest loads the full application context, including all beans and auto-configurations. This annotation is used for integration tests requiring the entire application. @WebMvcTest and @DataJpaTest only load part of the context (test slicing), while @ContextConfiguration is a lower-level annotation for manually configuring the context (more complex).
2What is test slicing in Spring Boot?
What is test slicing in Spring Boot?
답변
Test slicing means loading only a specific part of the Spring context to speed up tests. For example, @WebMvcTest loads only web components (controllers, filters), while @DataJpaTest loads only JPA repositories. This reduces startup time and improves test performance by avoiding loading unnecessary beans.
3Which annotation to use to test only a Spring MVC controller?
Which annotation to use to test only a Spring MVC controller?
답변
@WebMvcTest is specifically designed to test Spring MVC controllers. This annotation loads only web components (controllers, @ControllerAdvice, filters, interceptors) and automatically configures MockMvc. @SpringBootTest would load the entire context (unnecessary for testing a single controller), @DataJpaTest is for repositories, and @RestClientTest is for testing HTTP clients (RestTemplate, WebClient).
What is the purpose of @MockBean in Spring Boot Test?
Which object to use to simulate HTTP requests in a test with @WebMvcTest?
+27 면접 질문