Write Java unit tests with JUnit 5, AssertJ and Mockito

8 steps · about 1 h 58 min · Beginner

  1. Write your first JUnit 5 test class (guide, Baeldung, 15 min)

    Covers annotations, lifecycle methods and assertions in one short tour.

  2. Run JUnit 5 tests from Maven (doc, Apache Maven, 10 min)

    Shows the Surefire setup that makes mvn test find the JUnit Platform.

  3. Run JUnit 5 tests from Gradle (doc, Gradle, 15 min)

    Explains useJUnitPlatform, test filtering and reports for Gradle builds.

  4. Cover many inputs with parameterized tests (guide, Baeldung, 20 min)

    Replaces copy-pasted tests with value, CSV and method sources.

  5. Assert that code throws the right exception (guide, Baeldung, 8 min)

    Shows assertThrows and how to check the exception message.

  6. Write readable fluent assertions with AssertJ (doc, AssertJ, 20 min)

    Fluent assertions give clearer failure messages for collections and objects.

  7. Replace a dependency with a Mockito mock (doc, Mockito, 15 min)

    Lets you test one class without a real database or network call.

  8. Reuse setup with JUnit 5 extensions (guide, Baeldung, 15 min)

    Extensions share setup like temp resources or mocks across test classes.