JUnit 5 for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to JUnit 5



JUnit 5 is a modern framework for unit testing in the Java language. It is an evolutionary development of the popular JUnit 4 and is part of the Java ecosystem as the de facto standard for writing automated tests. JUnit 5 is not just an update, but a completely redesigned architecture consisting of three subprojects: JUnit Platform (test execution), JUnit Jupiter (new programming and extension model), and JUnit Vintage (backward compatibility with JUnit 3/4).



Why is JUnit 5 needed? In modern development (especially in TDD — Test-Driven Development methodologies), testing is not an option, but a mandatory stage. JUnit 5 allows you to:

  • Automate code verification: Run hundreds of tests with a single command.
  • Ensure regression reliability: Make sure new code hasn't broken old functionality.
  • Improve code design: Testable code often turns out to be more modular and loosely coupled.
  • Document behavior: Tests serve as living documentation, showing how a class or method should work.
  • Integrate into CI/CD: JUnit 5 easily integrates with Maven, Gradle, Jenkins, GitHub Actions.


Key innovations of JUnit 5 compared to JUnit 4: next-generation annotations (@Test, @ParameterizedTest, @Nested), a powerful extension system (Extensions), support for lambda expressions and streaming APIs, as well as built-in support for parameterized and dynamic tests.



Installing JUnit 5



Since Java is a compiled language, installation is done through dependency managers (Maven or Gradle). JUnit 5 is not installed via pip or npm. Below are instructions for the two most popular build systems.



Installation via Maven (pom.xml)

Add the following dependencies to your pom.xml file. It is recommended to use the latest stable version (at the time of writing — 5.11.0, check the latest on MVNRepository).

<dependencies>    <!-- Main JUnit Jupiter module (includes API, Engine, and parameterization) -->    <dependency>        <groupId>org.junit.jupiter</groupId>        <artifactId>junit-jupiter</artifactId>        <version>5.11.0</version>        <scope>test</scope>    </dependency>    <!-- (Optional) For compatibility with JUnit 4 -->    <dependency>        <groupId>org.junit.vintage</groupId>        <artifactId>junit-vintage-engine</artifactId>        <version>5.11.0</version>        <scope>test</scope>    </dependency></dependencies>


Installation via Gradle (build.gradle)

Add to the dependencies section:

dependencies {    testImplementation 'org.junit.jupiter:junit-jupiter:5.11.0'    // (Optional) For JUnit 4    testImplementation 'org.junit.vintage:junit-vintage-engine:5.11.0'}

test { useJUnitPlatform() // Required to enable JUnit 5}


Installation without a build system (Plain Java)

If you are not using Maven/Gradle, download the JAR files from the official repository and add them to your project's classpath. However, this is highly not recommended for real projects.



Quick Start: First Test in JUnit 5



Let's create a simple Calculator class and write a test for it.



1. Class to be tested (Calculator.java):

public class Calculator {    public int add(int a, int b) {        return a + b;    }

public int divide(int a, int

Recommendations