Integration tests
An integration test runs several real parts together: your code plus a database, a web layer, or a message broker. It checks the wiring that unit tests stub away.
Detailed Explanation
A unit test of a service with a fake repo cannot catch a wrong @Query, a missing Flyway migration, or a JSON field name the client does not send. An integration test starts enough of the system to exercise that path. In Spring, @SpringBootTest plus MockMvc or a TestRestTemplate hits HTTP and the context. @DataJpaTest slices the JPA layer. Testcontainers runs Postgres in Docker so the SQL is real.
Integration tests are slower and flakier if they share mutable state. Give each test a transaction that rolls back, or unique keys, or a fresh container. Do not depend on test order. Do not hit a shared "dev" database.
The test pyramid still applies. Many unit tests, fewer integration tests, a handful of end-to-end UI tests. Integration tests belong on the boundaries you do not trust: SQL, HTTP mapping, security filters, serialization.
You reach for an integration test when a bug would live in the connection between pieces. You keep a unit test for branching logic. You do not replace unit tests with a 40-second boot for every if.
The cost is CI time and Docker. The cost of too few is production-only schema bugs. The cost of @SpringBootTest on every class is a suite nobody runs. Slice tests (@WebMvcTest, @DataJpaTest) boot less.
Assert on HTTP status and a JSON path, or on a row in the database, not on every mock. If you mock the repo in a @SpringBootTest, you wrote a slower unit test.
Name tests after the contract: placeOrder_writesRowAndReturns201. Setup should create only the rows that test needs. Teardown is a rollback or a unique prefix, not a shared DELETE FROM that races another class. When a test needs Docker, mark it so a laptop without Docker can skip it. CI should run the marked suite. A suite that only passes on one engineer's machine is not an integration test you can ship.
Code Examples
Slice-style test without starting a server: the web layer plus a stub service.
// Slice-style test without starting a server: the web layer plus a stub service.
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(controllers = HealthController.class)
class HealthIT {
@Autowired
MockMvc mvc;
@Test
void healthIsOk() throws Exception {
mvc.perform(get("/health")).andExpect(status().isOk());
}
}
class HealthController {
@org.springframework.web.bind.annotation.GetMapping("/health")
String health() {
return "ok";
}
}import java.sql.Connection;
import java.sql.DriverManager;
public class JdbcPing {
static boolean ping(String url, String user, String pass) throws Exception {
try (Connection c = DriverManager.getConnection(url, user, pass)) {
return c.isValid(2);
}
}
}Common Gotchas
Shared databases plus unordered tests flake
isolate data.
Sleeping to "wait for async" is flaky
use latches, Awaitility, or poll with a timeout.
A `@SpringBootTest` that mocks every bean is a slow unit test
A `@SpringBootTest` that mocks every bean is a slow unit test