Unit vs Integration vs End to End Tests in Java Compared
Define unit, integration and end-to-end tests in a Java service by what each one starts, what it can catch and what it costs to run.

Teams argue about whether a test is "really" a unit test, and the argument ends when you describe the test by what it starts. This post defines the three types by that criterion for a Spring Boot service, shows one test of each and says what each can and cannot catch. It supports a practical Java testing strategy.
Unit test: nothing starts
A unit test runs plain Java. No Spring context, no database, no network, no files.
class ShippingCostTest {
@Test
void freeOverThreshold() {
var cost = new ShippingCost(5_000);
assertEquals(0, cost.forOrderTotal(7_500));
assertEquals(499, cost.forOrderTotal(1_000));
}
}It runs in a millisecond, fails only when the logic is wrong and its failure points at a method. It cannot catch a wrong SQL query, a mapping error or a misconfigured bean.
Integration test: real components, one boundary
An integration test starts real pieces that the unit test replaced, but not the whole system:
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
class OrderRepositoryTest {
@Container @ServiceConnection
static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:17");
@Autowired OrderRepository orders;
@Test
void findsOpenOrdersByCustomer() { /* save, query, assert */ }
}It starts a Spring slice and a real PostgreSQL, takes seconds and fails when the SQL, the mapping or the transaction behavior is wrong. It cannot tell you whether the web layer authenticates the caller. Variants include web slices (@WebMvcTest), message-broker tests and tests of a client against a stub server.
End-to-end test: the deployed system from outside
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class CheckoutJourneyTest {
@Autowired TestRestTemplate http;
@Test
void customerCanPlaceAnOrder() {
var response = http.postForEntity("/api/orders", new CreateOrder("c1", List.of(new CreateOrder.Line("sku-1", 1))), OrderView.class);
assertEquals(HttpStatus.CREATED, response.getStatusCode());
}
}This starts the whole application and calls it over HTTP. Some teams run the same journey against a deployed environment. It takes many seconds, depends on everything, and fails for reasons that include the environment. It is the only test that proves the pieces work together as users meet them.
Compare them
| Unit | Integration | End to end | |
|---|---|---|---|
| Starts | Nothing | Framework slice and real infrastructure | Whole application |
| Speed | milliseconds | seconds | seconds to minutes |
| Catches | Logic | Wiring, SQL, serialization | Journeys, configuration |
| Misses | Everything outside the class | Cross-component behavior | Fine detail, because it is too coarse |
| Failure points at | A method | A component | Somewhere |
Where the lines blur
A @SpringBootTest with all mocks is not a unit test, because it starts the context, and it is not an integration test either, because the real parts are missing. A test of a service class that uses an in-memory fake repository is a unit test, even though it exercises two classes. A contract test sits between integration and end to end, checking an interface without deploying both sides. Name tests by purpose, and separate packages or Maven profiles (*Test for unit, *IT for integration) so a developer can run the fast set alone.
Count what each kind of test costs
The three labels are easiest to compare by what they cost, and a quick measurement settles it for your project. Run the suites separately with Maven's surefire and failsafe plugins and note the wall-clock time of each phase, and the test count each prints at the end.
time ./mvnw -q test # unit tests only, run by surefire
time ./mvnw -q verify # adds the integration tests, run by failsafeA healthy shape has hundreds of unit tests running in seconds, tens of integration tests in a minute or two, and a handful of end to end tests that run after a deploy. If the unit suite takes minutes, some of its tests start a Spring context or a database and are really integration tests with a unit test's name. If the integration suite has more tests than the unit suite, business logic is buried in framework code, and no test can reach it without the framework.
Use the same command at each pull request and compare. A suite that grows slower by a few seconds every week is the one that developers eventually stop running locally.
Decide the level by what can break
The right level for a test depends on where the defect would be. A discount rule that rounds wrongly lives in a plain class, and a unit test finds it in a millisecond. A query that returns the wrong rows, or a transaction that does not roll back, lives at the boundary with the database, and only a test with the database finds it. A misconfigured route, a missing header in the gateway or a wrong environment variable lives in the deployment, and only a test of the deployed system finds it.
// Unit: the rule, with no framework
@Test
void tenPercentOffOverOneHundred() {
assertThat(new Discount().apply(new BigDecimal("120.00"))).isEqualByComparingTo("108.00");
}
// Integration: the query, with a real database
@Test
void findsOpenOrdersForCustomer() {
repository.save(order("c1", OPEN));
repository.save(order("c1", PAID));
assertThat(repository.findOpenByCustomer("c1")).hasSize(1);
}When a bug gets through, ask which level should have caught it and add the test there. Adding an end to end test for a rounding bug works, and costs a minute of CI time forever.
Make integration tests honest about their boundary
An integration test should replace what is outside the boundary and keep what is inside. A test of the order repository uses a real database and no web layer. A test of the controller uses MockMvc with the service mocked. Mixing both in one test, a real database, a real web server and a mocked payment gateway, is the slow flaky style that gives integration tests their reputation. Keep each test to one boundary, and the failures say where to look.
Start containers once for the whole test run. A shared PostgreSQL container, started in a base class or through a Spring @ServiceConnection, costs a few seconds once. A new container per class costs that every time, and a suite of fifty classes spends minutes starting databases.
Keep end to end tests as a smoke check
End to end tests answer a different question: is the deployed thing alive and wired correctly? They should be few, read-only where possible, and run against a real environment after each deploy. A short script that logs in, creates an order in a test account and reads it back covers most of the wiring.
set -e
token=$(curl -s -X POST "$BASE/auth/token" -d "user=smoke&password=$SMOKE_PASSWORD" | jq -r .token)
id=$(curl -s -X POST "$BASE/orders" -H "Authorization: Bearer $token" -H 'Content-Type: application/json' -d '{"sku":"A-1"}' | jq -r .id)
curl -sf "$BASE/orders/$id" -H "Authorization: Bearer $token" | jq -e ".id == \"$id\""The script fails on the first non-2xx answer, and it leaves behind one order in the smoke account, which a nightly job cleans up.
A review checklist for a test suite
Once a quarter, review the suite against four questions. Which tests failed in the last three months and for what reason: a real bug, a flaky condition, or a change in the test? Flaky tests are a cost to remove, not tolerate. Which parts of the code were changed most often and what tests cover them? Frequent change with few tests is where the next bug appears. How long does the slowest ten percent of tests take, and are they integration tests? Slow tests deserve a look at whether they need the whole stack. And which tests have not failed in a year and cover trivial code? Those are candidates for deletion, since every test costs maintenance.
Keep a short document that states the test strategy: what is tested at which level, what the time budget for each stage is, and what has to pass to merge. A strategy that is written down is argued about once, instead of every pull request.
Where the definitions do not help
A definition cannot tell you how many of each to write; that depends on where your defects are, which the test pyramid post discusses. For the framework features used in these examples see the JUnit 5 handbook. The Spring Boot testing reference lists the slices, and Maven Failsafe runs *IT classes in a separate phase.


