Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is the test pyramid, and how do Spring Boot slice tests such as @WebMvcTest and @DataJpaTest fit into it?

The test pyramid is a guideline for the shape of a test suite: many fast unit tests at the bottom, fewer integration tests in the middle, and only a few slow end-to-end tests at the top. Spring Boot slice tests such as @WebMvcTest and @DataJpaTest start only part of the application context, the MVC infrastructure or the JPA repositories, so they sit between plain unit tests and full @SpringBootTest tests in both cost and scope. Other models, such as the testing trophy, put more weight on integration tests; the common point is balancing cost against confidence.

Why this question is asked

Every team has to decide how many tests of each kind to write, and getting the shape wrong is expensive in either direction. A suite of only end-to-end tests is slow and flaky; a suite of only mocked unit tests is fast but misses the bugs that live between components. Interviewers ask about the pyramid because it is the shared vocabulary for this decision, and asking about slice tests checks whether we know the tools Spring Boot gives us for the middle layer. In this answer, we will explain the pyramid and the reasoning behind it, look at what a slice test actually loads with two examples, place slices in the pyramid, and compare the pyramid with newer models such as the testing trophy.

1A Quick Recap of the Test Pyramid

The pyramid describes three levels of tests. Unit tests check a small piece of code, such as one class or a few classes, with no framework, no database and no network; they run in milliseconds. Integration tests check that several parts work together, such as our code with a real database or with the web framework. End-to-end tests drive the whole deployed system the way a user would, through the API or the browser.

The shape comes from cost. As we go up, each test gets slower to run, harder to write, more likely to fail for reasons unrelated to our change, and harder to debug when it fails, because a failure could be anywhere in the stack. But each test also checks more of the real system. The pyramid's advice is to check each behaviour at the lowest level that can check it, which naturally gives many unit tests, fewer integration tests and only a few end-to-end tests for the paths that truly need the whole system.

Step throughThe test pyramid with Spring Boot tools
  1. End-to-end testsA few critical journeys against a deployed system; minutes
  2. Full integration tests@SpringBootTest with real HTTP, database and broker in containers; seconds
  3. Slice tests@WebMvcTest, @DataJpaTest, @JsonTest: part of the context; around a second
  4. Unit testsPlain JUnit and Mockito, no Spring context; milliseconds

4 layers, from the top down. Step through them one at a time.

Going up: more realism per test, but slower, flakier and harder to debug.

2What a Slice Test Loads

A full @SpringBootTest starts the whole application context: every controller, service, repository, client, scheduler and listener, plus all the auto-configuration. A slice test starts only the beans that belong to one layer. Spring Boot does this by switching on a limited set of auto-configurations and filtering which of our components are scanned.

@WebMvcTest loads the Spring MVC infrastructure, our controllers, @ControllerAdvice classes, JSON converters, filters and the security configuration if present, and gives us MockMvc. It does not load services or repositories, so we provide them as mocks. @DataJpaTest loads JPA, Hibernate, Spring Data repositories and a data source, wraps each test in a transaction that rolls back, and does not load controllers or services.

Java
@WebMvcTest(OrderController.class) // only the web layer, and only this controller
class OrderControllerTest {
@Autowired MockMvc mvc;
@MockitoBean OrderService service; // Spring Boot 3.4+; older versions use @MockBean
@Test
void returns404ForUnknownOrder() throws Exception {
when(service.find(99L)).thenThrow(new OrderNotFoundException(99L));
mvc.perform(get("/orders/99")).andExpect(status().isNotFound()); // checks mapping and error handling
}
}
@DataJpaTest // only JPA: entities, repositories, a data source
class OrderRepositoryTest {
@Autowired OrderRepository orders;
@Test
void findsOpenOrders() {
orders.saveAndFlush(Order.open("A1"));
assertThat(orders.findByStatus(OrderStatus.OPEN)).hasSize(1); // real SQL, real mapping
}
}

Notice what each test checks. The controller test verifies request mapping, the exception handler and the HTTP status, which a plain unit test of the controller method could not, because it would skip Spring MVC entirely. The repository test verifies the entity mapping and the generated query against a real database, which a mocked repository never could. Neither test pays for the rest of the application. By default @DataJpaTest uses an embedded database if one is available; to test against the production engine we combine it with Testcontainers and @AutoConfigureTestDatabase(replace = NONE).

3Where Slices Sit in the Pyramid

Slice tests are integration tests in the sense that they involve the framework and real infrastructure, but they are much cheaper than a full @SpringBootTest. A slice context usually starts in well under a second to a couple of seconds, against several seconds or more for a large application's full context, and it is cached and reused across test classes with the same configuration.

So slices give us a lower, wider part of the integration layer. Most web layer behaviour, validation, status codes, error bodies, JSON field names, can be tested with @WebMvcTest; most persistence behaviour with @DataJpaTest or @JdbcTest; JSON serialisation alone with @JsonTest; and HTTP clients with @RestClientTest. That leaves full @SpringBootTest tests for the smaller set of behaviours that really need everything wired together, such as a request flowing from controller to database, or security across the whole chain.

There is a catch to be aware of. Each different slice configuration, for example a different set of @MockitoBean declarations, creates a new application context, and too many distinct contexts make the suite slow. Keeping slice configurations consistent lets Spring's context cache do its work.

4The Testing Trophy and Other Shapes

The pyramid is not the only model. The testing trophy, popularised in the JavaScript world, puts static analysis at the base, a modest layer of unit tests, a large layer of integration tests and a small top of end-to-end tests. Its argument is that integration tests give the most confidence per unit of effort, because users experience integrated behaviour, and modern tools have made integration tests much cheaper. For microservices, the testing honeycomb makes a similar argument: a service has little logic of its own and many interactions, so most tests should be integration tests of the service with its real dependencies.

These models disagree about proportions, not about principles. Every one of them says that tests should be chosen for the confidence they give relative to their cost, and that the slow, fragile end-to-end tests should be few. Spring Boot's slices and Testcontainers are a big reason why the middle layer has become cheaper, which is why many Spring teams today end up with a shape closer to the trophy than to the classic pyramid. The right shape for a service depends on where its risk lives: a pricing engine full of rules wants many unit tests; a thin service that mostly moves data between systems wants more integration tests.

5Follow-up questions

Is a @WebMvcTest a unit test or an integration test?

It is best called a slice or component test. It tests the controller together with the real Spring MVC machinery, so it is more than a unit test, but it mocks everything below the web layer, so it is far narrower than a full integration test. The label matters less than knowing what it does and does not exercise.

Why not test controllers by calling the methods directly?

Because most of what can go wrong in a controller is in the framework's handling around it: request mapping, path variables, request body conversion, validation, exception handlers and response serialisation. A direct method call skips all of that. @WebMvcTest exercises it while staying fast.

How many end-to-end tests are enough?

Enough to cover the few journeys whose failure would be most costly, and that cannot be verified at a lower level, such as signing up, placing an order and paying. For most services that is a handful per journey, not hundreds. Everything else should be pushed down to slices, integration and contract tests.

6Conclusion

In this answer, we saw that the test pyramid recommends many fast unit tests, fewer integration tests and few end-to-end tests, because cost, flakiness and debugging effort grow as tests cover more of the system. Spring Boot slice tests such as @WebMvcTest and @DataJpaTest start only one layer of the context, so they sit between unit tests and full @SpringBootTest tests in cost and scope and make a large part of integration testing cheap. Models such as the testing trophy shift weight toward integration tests, but all of them share the same idea: choose each test for the confidence it gives relative to what it costs.

What to say in the interviewAbout 90 seconds aloud

Open with

The test pyramid says to have many fast unit tests at the bottom, fewer integration tests in the middle and a few end-to-end tests at the top. The reason is cost: as tests cover more of the system they get slower, flakier and harder to debug, so we check each behaviour at the lowest level that can check it and keep end-to-end tests for critical journeys.

Then, if they want more

Spring Boot's slice tests fit in the middle. A slice starts only part of the context. @WebMvcTest loads the MVC infrastructure, the controller, advice, JSON converters and security, and gives me MockMvc, while services are mocked with @MockitoBean, or @MockBean before Boot 3.4.

So I can test mapping, validation, status codes and error bodies in about a second. @DataJpaTest loads only JPA, repositories and a data source, with rollback after each test, so I can test mappings and queries, ideally against the real database with Testcontainers.

That makes slices cheaper than a full @SpringBootTest but much more realistic than a plain unit test, so I use them for most web and persistence behaviour and keep full context tests for flows across layers. I'd also mention that the pyramid isn't the only model.

The testing trophy and the microservice honeycomb put more weight on integration tests, because tools like slices and Testcontainers made them cheap. The common idea is choosing tests by confidence versus cost.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.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.
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 is the test pyramid, and how do Spring Boot slice tests such as @WebMvcTest and...": https://scrivio-interview-library.netlify.app/java/test-strategy/b7fc332cad/