Spring Boot

Spring Boot Testing

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

30 preguntas de entrevista·
Mid-Level
1

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

Respuesta

@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?

Respuesta

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?

Respuesta

@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 preguntas de entrevista

Domina Spring Boot para tu próxima entrevista

Accede a todas las preguntas, flashcards, tests técnicos, ejercicios de code review y simuladores de entrevista.

Empieza gratis