Sub topic 1
What to test and at which level
Prerequisites
This chapter assumes you can write a basic JUnit-style test in Java and have built a small Spring Boot application with a REST controller and Spring Data JPA. You should know what a Spring bean is and how dependency injection wires beans together. You should also be able to add a dependency in Maven or Gradle.
The working model: cost against confidence
Every test trades speed for confidence. A test that runs in milliseconds and touches one class tells you little about how the parts fit together. A test that starts the whole application and a real database tells you a lot, but it takes seconds and breaks for more reasons.
The test pyramid is a common way to balance this. You write many cheap tests at the bottom and few expensive tests at the top.
The three levels in Spring Boot
Spring Boot gives you a clear tool for each level.
| Level | What it loads | Typical annotation | Good for |
|---|---|---|---|
| Unit test | One class, with collaborators mocked | `@ExtendWith(MockitoExtension.class)` | Business logic in services |
| Slice test | One layer of the application | `@WebMvcTest`, `@DataJpaTest` | Request mapping, queries |
| Full-context test | The whole application context | `@SpringBootTest` | Wiring, configuration, end-to-end flows |
A unit test creates the class under test directly with new or with Mockito. No Spring code runs.
A slice test starts a Spring context, but only the beans for one layer, such as the web layer or the JPA layer. Other beans are absent or replaced by mocks.
A full-context test starts every bean, just as the real application would.
Choosing a level
Ask what could go wrong in the code you are about to test. Then pick the cheapest level that can detect that failure.
- Does the code contain logic, such as calculations, branching, or validation? Use a unit test. A service method that picks a discount belongs here.
- Does the risk lie in how Spring maps the code to something else? Use a slice test. Examples are URL and status-code mapping in a controller, JSON binding, or a derived query method in a repository.
- Does the risk lie in how pieces are wired or configured together? Use a full-context test. Examples are a missing bean, a wrong property, or a security filter chain that blocks a path.
A simple rule helps. If the test would pass without Spring doing anything, it should not load Spring.
Worked example
Take a greeting feature with a GreetingController, a GreetingService, and a GreetingRepository. One reasonable plan looks like this:
- A unit test for
GreetingServicewith a mocked repository. It checks the logic for building the greeting text. - A
@WebMvcTestforGreetingControllerwith a mocked service. It checks thatGET /greetings/Adareturns status 200 and the right body. - A
@DataJpaTestforGreetingRepository. It checks thatfindByNamereturns the saved row. - One
@SpringBootTestthat calls/actuator/healthover HTTP. It proves the whole application starts and is wired correctly.
Each test answers one question. Most of the logic is covered by the fastest tests, and only one test pays the cost of the full application.
Common mistakes
- Using
@SpringBootTestfor everything. The suite becomes slow, and failures are harder to trace to one cause. - Mocking everything in a slice test. If a controller test mocks so much that it checks nothing but the mocks, it has no value.
- Testing the framework. Do not write a test that only proves Spring can inject a bean or that a JPA
savecall stores a row. Test your own behavior. - Skipping the top of the pyramid. Unit and slice tests cannot catch a missing bean or a bad property. Keep a few full-context tests.
Exercise
A PriceCalculator service applies a 10% discount to orders over 100 and calls no other bean. Which test level fits best, and why?
Show answer
A unit test. The code is pure logic with no dependency on Spring, so it can be tested directly in milliseconds without loading a context. A slice or full-context test would add cost without detecting any extra failure.
Exercise
A controller returns 404 for an unknown name, but a client reports it receives 500. The service and repository tests all pass. Which level can reproduce the problem most cheaply?
Show answer
A @WebMvcTest slice test with a mocked service. It exercises the request mapping and exception handling in the web layer without starting a database.