Repaso
Español
Spring, JPA & Java WebIntermediateSPR-24

Which test annotation starts the FULL Spring application context, and which loads only the web layer?

Options

  1. @WebMvcTest starts the full context; @SpringBootTest loads the web layer
  2. @SpringBootTest starts the full context; @WebMvcTest loads the web layer (Correct option)
  3. Both annotations always load the whole application context
  4. @DataJpaTest starts the full context of the application

Answer

@SpringBootTest is full; @WebMvcTest loads only controllers and web components (with MockMvc)

Why

@SpringBootTest starts the whole application: slow but realistic (integration tests).

Test slices load only the part you are testing, so they are faster:

  • @WebMvcTest(UserController.class): only the web layer; you mock services and call endpoints with MockMvc.
  • @DataJpaTest: only JPA and repositories, with an in-memory database by default.

For pure unit tests of a service, skip Spring: use JUnit + Mockito.

Remember

Slices (@WebMvcTest, @DataJpaTest) are fast; @SpringBootTest is the full app.

Key terms

Practice this question in Repaso