Write Java unit tests with JUnit 5, AssertJ and Mockito
8 steps · about 1 h 58 min · Beginner
- Write your first JUnit 5 test class (guide, Baeldung, 15 min)
Covers annotations, lifecycle methods and assertions in one short tour.
- Run JUnit 5 tests from Maven (doc, Apache Maven, 10 min)
Shows the Surefire setup that makes mvn test find the JUnit Platform.
- Run JUnit 5 tests from Gradle (doc, Gradle, 15 min)
Explains useJUnitPlatform, test filtering and reports for Gradle builds.
- Cover many inputs with parameterized tests (guide, Baeldung, 20 min)
Replaces copy-pasted tests with value, CSV and method sources.
- Assert that code throws the right exception (guide, Baeldung, 8 min)
Shows assertThrows and how to check the exception message.
- Write readable fluent assertions with AssertJ (doc, AssertJ, 20 min)
Fluent assertions give clearer failure messages for collections and objects.
- Replace a dependency with a Mockito mock (doc, Mockito, 15 min)
Lets you test one class without a real database or network call.
- Reuse setup with JUnit 5 extensions (guide, Baeldung, 15 min)
Extensions share setup like temp resources or mocks across test classes.