Unit Tests vs Integration Tests in Java: Key Differences
Unit tests check one class in isolation with mocks; integration tests check real components together. See examples, a comparison table and when to use each.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Unit tests check one class in isolation with mocks; integration tests check real components together. See examples, a comparison table and when to use each.
Compare MockMvc, TestRestTemplate and WebTestClient, then test status codes, JSON bodies, errors and security against a real database in Spring.
@Rollback controls whether a transactional Spring test rolls back or commits. See class and method usage, @Commit, TestTransaction and cases where rollback does not apply.
Annotate tests with @Transactional so Spring rolls back each test automatically, and learn when to commit, flush or clean up manually.
@DirtiesContext marks the cached Spring test context as dirty so it is rebuilt for the next test. Learn when to use it and what to use instead.
Three ways to write custom assertions for JUnit tests: static helpers, AssertionFailedError and fluent AssertJ assertion classes.
@TestPropertySource overrides configuration for a Spring test with property files or inline values. See its precedence, usage and alternatives.
Create application-test.properties, activate it with ActiveProfiles, swap beans with Profile or TestConfiguration, and use Testcontainers for real databases.
H2 gives Spring tests a fast in-memory database. Learn setup with @DataJpaTest and @Sql, plus when Testcontainers is the safer choice.
Use H2 with @DataJpaTest and @SpringBootTest for fast, isolated database tests in Spring Boot, and learn where in-memory databases differ from production.
It makes a Spring Boot test build its context from the classes you list instead of the discovered application class, giving focused and faster integration tests.
Use @DataJpaTest to run repository tests against a real database in a rolled-back transaction. Learn flushing, custom query tests and Testcontainers setup.
@DataJpaTest loads only the JPA slice of a Spring Boot app so you can test repositories and queries fast, with an embedded or Testcontainers database.
Use @WebMvcTest, MockMvc and a mocked service to test controller responses without a server, plus how to fake external HTTP calls.
@WebMvcTest isolates the web layer in Spring tests. Compare it with other test slices, see a MockMvc example, and learn how to handle security and mocks.
Add JaCoCo to Maven or Gradle, run the tests and read the HTML report. Learn exclusions, coverage thresholds and how to publish in CI.
Learn how to structure tests for Spring Boot: fast unit tests, slice tests, Testcontainers integration tests and habits that keep the suite reliable.
Plan and run load, stress and soak tests on a Spring application with Gatling, JMeter or k6, and find the bottlenecks they reveal.
The @SpringBootTest annotation starts the full Spring Boot application context so you can write integration tests, with options for web mode, properties and mocks.
Wait on the returned CompletableFuture with a timeout, poll with Awaitility for void methods, and verify exceptions and threads when testing @Async code in Spring.