Java Unit Testing with JUnit 6 and Mockito

Testing with JUnit is an intermediate-level course of 5 lessons (about 2 h 25 min), with 15 exercises and 15 quiz questions. Prerequisites: Collections, Streams, and Exceptions. Exercises are run and checked in a server-side sandbox.

  • Java
  • Intermediate
  • 5 lessons
  • 2 h 25 min
  • 15 exercises
  • Exercises run in a server sandbox

The course in brief

This course teaches you to secure your Java code with automated unit tests. You will write JUnit 6 tests structured as Arrange / Act / Assert, test error cases and edge cases, factor out setup with the JUnit lifecycle, and isolate your services using test doubles and Mockito. The exercises reproduce JUnit's logic in plain Java, while the examples show the real code to use in your Maven projects.

What you will be able to do

  • Write readable JUnit 6 unit tests named after the behavior being tested
  • Use the right assertions, including assertThrows, assertAll, and tolerances for decimals
  • Organize tests with @BeforeEach, @Nested, and @DisplayName while guaranteeing their isolation
  • Cover edge cases with parameterized tests (@ValueSource, @CsvSource, @MethodSource)
  • Isolate a class from its dependencies with fakes and Mockito mocks

Syllabus: 5 lessons

    • Explain what an automated unit test brings
    • Write a first JUnit 6 test structured as Arrange / Act / Assert
    • Run tests with Maven or Gradle and read the report

    Unit testsJUnit 6 (Jupiter)Arrange / Act / Assert structure

  1. 02

    JUnit Assertions and Testing Exceptions

    18 min
    • Choose the right assertion: assertEquals, assertTrue, assertNull, assertIterableEquals
    • Compare decimal numbers with a tolerance
    • Verify that an exception is thrown with assertThrows and group checks with assertAll

    JUnit assertionsassertThrowsassertAll

  2. 03

    Test Lifecycle and Organization

    16 min
    • Set up a fresh state before each test with @BeforeEach
    • Understand the execution order of the lifecycle annotations
    • Organize a test class with @DisplayName, @Nested, and @Disabled

    JUnit lifecycleTest isolation@Nested and @DisplayName

  3. 04

    Parameterized Tests

    16 min
    • Run the same test on several datasets with @ParameterizedTest
    • Choose the right source: @ValueSource, @CsvSource, @MethodSource, @EnumSource
    • Cover edge cases systematically

    Parameterized tests@CsvSourceEdge cases

  4. 05

    Isolating Code: Test Doubles and Mockito

    20 min
    • Make a class testable through constructor dependency injection
    • Distinguish fake, stub, mock, and spy
    • Write a Mockito test with @Mock, when/thenReturn, and verify

    Test doublesMockitoDependency injection

Final project

Test Suite for an Order Service

You are given (or you write) a CommandeService that computes an order's total (tiered discounts, free shipping from €50), rejects invalid orders with exceptions, and calls a repository and an email service. Write its complete test suite in a Maven project: unit tests of the pricing rules, parameterized tests of the tiers at the boundaries, exception tests, and Mockito tests of the interactions with the repository and the email service.

Deliverables

  • A Maven project with CommandeService in src/main/java and its tests in src/test/java
  • At least 12 tests, including 2 parameterized tests, 3 exception tests, and 2 tests using Mockito
  • The output of ./mvnw test showing all tests green
  • A short paragraph listing a bug found (or deliberately introduced) and the test that detects it

Assessment criteria

  • Test names clearly describe the behavior being verified
  • Edge cases (tier boundaries, empty order, negative quantity) are covered
  • Tests are independent (setup in @BeforeEach, no shared mutable state)
  • Mocks are limited to external dependencies and verify is used appropriately
  • All tests pass with ./mvnw test

Frequently asked questions

JUnit 5 or JUnit 6: which one should I learn?

The course uses JUnit 6, the current version, with Java 25. JUnit 5 and JUnit 6 share the same Jupiter API (org.junit.jupiter.api): @Test, assertions, and parameterized tests are written the same way. JUnit 6 requires Java 17 or later and aligns the version numbers of all its modules; what you learn also applies to a project that is still on JUnit 5.

Do I need to install JUnit to follow the course?

Not for the platform's exercises, which reproduce JUnit's logic in plain Java. For your own projects and the setup exercise, you will add the junit-jupiter dependency (via the junit-bom 6.x BOM) to a Maven or Gradle project.

What is Mockito, and should I always use it?

Mockito creates test doubles to replace dependencies (database, email, external API). It is very useful, but a simple in-memory fake is sometimes more readable: the course teaches you how to choose.

Does this course cover TDD?

Yes, the red / green / refactor cycle is introduced in the very first lesson and can be applied in the final project.

This course, told for your job

Say what you want to be able to do: “Testing with JUnit” takes its place in your path, with examples from your field. The concepts stay those of the course.

The preview is free, and you can take the courses one at a time without paying. The path only contains lessons from the catalogue.