Testingmedium5-8 years

When would you use @WebMvcTest or @DataJpaTest instead of @SpringBootTest, and what's the actual difference?

@SpringBootTest boots the whole application context (or a meaningful slice of it, e.g. webEnvironment = RANDOM_PORT for a real embedded server) — slow but realistic, good for a handful of true integration tests. @WebMvcTest and @DataJpaTest are slice tests: they load only the beans relevant to one layer (the web layer with MockMvc, or the JPA layer with an in-memory or test database) and auto-configure just enough infrastructure for it, which is both much faster and forces you to mock out everything the slice deliberately excludes — collaborators you'd otherwise have to wire for real.