Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What does @WebMvcTest load and not load, and how does it differ from @DataJpaTest?

@WebMvcTest loads only the Spring MVC infrastructure: controllers, @ControllerAdvice, converters, filters, Jackson and web configuration, security auto-configuration where relevant, and a MockMvc to call them. It does not load services, repositories or other components, so we mock those with @MockitoBean. @DataJpaTest is the persistence counterpart: it loads JPA repositories, an EntityManager and Flyway or Liquibase, makes each test transactional with rollback, and by default swaps the DataSource for an embedded one unless we say otherwise.

Why this question is asked

Test slices are how Spring Boot lets us test one layer of an application without paying for the whole context. They start much faster than @SpringBootTest and keep a failure close to its cause. But a slice only works if we know what is inside it; most confusing slice failures come from expecting a bean that the slice deliberately leaves out. Interviewers ask this to check that we understand slices as filters on the context. In this answer, we will recap what a slice is, look at what each annotation loads and leaves out, with a test for each, and compare the two.

1A Quick Recap of Test Slices

A test slice is an annotation that starts a reduced application context containing only the parts relevant to one layer. It does two things. It filters component scanning, so only certain kinds of our classes become beans, and it applies only a chosen list of auto-configurations, the Spring Boot classes that set up infrastructure such as Jackson, MVC or JPA. Everything else is simply not there.

Because the context is small, it starts in a second or two rather than ten, and because it holds only one layer, a failing test points straight at that layer.

2What @WebMvcTest Loads and Leaves Out

@WebMvcTest scans only web-related classes: @Controller and @RestController classes, @ControllerAdvice, Jackson components, Converter and GenericConverter, Filter, HandlerInterceptor, WebMvcConfigurer and HandlerMethodArgumentResolver beans. It applies the MVC, Jackson, validation and, when Spring Security is on the classpath, the security auto-configuration, and it provides a MockMvc bean. We can name one controller, @WebMvcTest(OrderController.class), to narrow it further.

What it leaves out is everything a controller usually calls: @Service, @Repository and plain @Component classes. A controller that needs OrderService would fail with a NoSuchBeanDefinitionException, so we supply a mock.

Java
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired MockMvc mvc;
@MockitoBean OrderService orders; // not loaded by the slice, so we mock it
@Test
void returnsOrder() throws Exception {
given(orders.find(42L)).willReturn(new Order(42L, "NEW"));
mvc.perform(get("/orders/42"))
.andExpect(status().isOk()) // 200
.andExpect(jsonPath("$.status").value("NEW"));
}
}

Notice that a security configuration written as a @Configuration class is not picked up by the scanning filter, because a configuration class is not one of the web types. When the endpoint's security rules matter to the test, we @Import our security configuration so the test runs against the real rules; check what your slice loads in your version.

3What @DataJpaTest Loads and Leaves Out

@DataJpaTest scans only JPA entities and Spring Data repositories, and applies the DataSource, JPA, transaction and Flyway or Liquibase auto-configurations. It also gives us a TestEntityManager for setting up data. Controllers, services and web infrastructure are left out.

Two behaviours surprise people. Each test method runs in a transaction that is rolled back at the end, so tests do not see each other's data. And by default the slice replaces our configured DataSource with an embedded in-memory database such as H2, if one is on the classpath. To test against the real engine, we keep our own DataSource and point it at a container.

Java
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
class OrderRepositoryTest {
@Container @ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17");
@Autowired OrderRepository repo;
@Test
void findsOpenOrders() {
repo.save(new Order(null, "NEW"));
assertThat(repo.findByStatus("NEW")).hasSize(1); // 1, rolled back after the test
}
}

Notice replace = NONE, which tells the slice not to swap in an embedded database. In recent Spring Boot versions a DataSource that comes from a @ServiceConnection container is left alone even without it; check your version, and keep replace = NONE when you want the intent to be explicit.

4Comparing the Two Slices

Both slices follow the same idea, a narrow context for one layer, but they differ in what they include, what we must replace, and how test data behaves. The table puts them side by side.

@WebMvcTest compared with @DataJpaTest
Aspect@WebMvcTest@DataJpaTest
Beans scannedControllers, advice, filters, convertersEntities and repositories
Auto-configurationMVC, Jackson, validation, securityDataSource, JPA, transactions, migrations
Left outServices, repositories, componentsWeb layer, services
Usually mockedServices the controller callsNothing, the database is the point
TransactionsNone by defaultEach test rolled back
Test toolsMockMvc, MockMvcTesterTestEntityManager

5A Note on Spring Boot 4

Spring Boot 4 split its auto-configuration into smaller modules, and the test slice annotations moved with them into module-specific packages and test starters, for example a web MVC test module and a data JPA test module. The annotations behave the same way, but imports and dependencies change during an upgrade, so check your version's migration notes when a slice annotation no longer resolves.

6Follow-up questions

Can I use @WebMvcTest and @DataJpaTest on the same class?

No, each slice brings its own test bootstrapper and they conflict. If a test needs both the web layer and real persistence, it is an integration test, and @SpringBootTest with MockMvc fits better.

Why does my @DataJpaTest not see data committed by a migration?

It should: Flyway or Liquibase runs when the slice starts, before tests, and its data is committed. What tests do not see is data from other tests, because each test's changes are rolled back.

How do I add a bean that the slice leaves out but my controller really needs?

Either mock it with @MockitoBean, or import the real class with @Import if it is light and has no further dependencies, such as a mapper. Importing a whole service usually pulls in repositories and defeats the slice.

7Conclusion

In this answer, we saw that a slice filters component scanning and auto-configuration down to one layer. @WebMvcTest loads controllers, advice, converters, filters, Jackson and web and security configuration with MockMvc, and leaves out services and repositories, which we mock with @MockitoBean. @DataJpaTest loads repositories, the EntityManager and migrations, rolls back each test, and swaps the DataSource for an embedded one unless we use replace = NONE with a container. In Spring Boot 4 the annotations moved to module-specific packages.

What to say in the interviewAbout 70 seconds aloud

Open with

Both are test slices, which means they start a small context for one layer by filtering which of my classes are scanned and which auto-configurations apply. @WebMvcTest loads the MVC layer: controllers, @ControllerAdvice, converters, filters, interceptors, WebMvcConfigurer, Jackson and validation, plus security auto-configuration if Spring Security is present, and it gives me a MockMvc.

Then, if they want more

It does not load services, repositories or plain components, so I mock what the controller calls with @MockitoBean.

If my security rules live in a configuration class, I import it so the test checks the real rules. @DataJpaTest is the persistence slice: it loads entities, Spring Data repositories, the EntityManager and Flyway or Liquibase, gives me a TestEntityManager, and wraps each test in a transaction that is rolled back.

By default it replaces my DataSource with an embedded database like H2, which I usually do not want, so I use @AutoConfigureTestDatabase with replace NONE and a PostgreSQL Testcontainers container with @ServiceConnection, to test against the real engine.

One upgrade note: in Spring Boot 4 the slice annotations moved into module-specific packages, so imports change, but the behaviour is the same.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference, Testing
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does @WebMvcTest load and not load, and how does it differ from @DataJpaTest?": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/dc549fa2f9/

More in Spring Boot testing