Spring Boot

Spring Boot Testing

Spring Boot testing: @SpringBootTest, @WebMvcTest, @DataJpaTest, MockMvc, TestRestTemplate, test slices

30 câu hỏi phỏng vấn·
Mid-Level
1

Which annotation to use to load the full Spring Boot context for an integration test?

Câu trả lời

@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).

2

What is test slicing in Spring Boot?

Câu trả lời

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.

3

Which annotation to use to test only a Spring MVC controller?

Câu trả lời

@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).

4

What is the purpose of @MockBean in Spring Boot Test?

5

Which object to use to simulate HTTP requests in a test with @WebMvcTest?

+27 câu hỏi phỏng vấn

Nắm vững Spring Boot cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí