VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/introduction-to-junit-5/

⇱ Introduction to JUnit 5 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to JUnit 5

Last Updated : 26 Sep, 2025

JUnit is used testing framework for Java applications. JUnit 5, also known as JUnit Jupiter, is the latest version of the framework. Compared to JUnit 4, it provides more features, flexibility and a modern architecture for writing and executing tests.

JUnit 5 is composed of three main modules:

  1. JUnit Platform
  2. JUnit Jupiter
  3. JUnit Vintage
👁 junit_platform_

These components collectively make it easier to define, organize and run tests efficiently.

1. JUnit Platform

The JUnit Platform is the foundation of JUnit 5. It provides a launching mechanism for running tests on the JVM. In real-world applications, the testing team designs various test cases to verify the functionality and behavior of an application. The JUnit Platform ensures that these test cases can be executed consistently on any Java Virtual Machine (JVM).

2. JUnit Jupiter

JUnit Jupiter is the module that provides new programming models and features for writing test cases in JUnit 5. It includes annotations, assertions, assumptions and more, enabling developers to write modern, clean and maintainable test code.

3. JUnit Vintage

JUnit Vintage allows you to run legacy tests written with JUnit 3 or JUnit 4 on the JUnit 5 platform. This ensures backward compatibility during the transition to JUnit 5.

Basics of JUnit 5

JUnit provides many features compared to JUnit 4. If you want to learn JUnit 5, then we need to know some basics of the JUnit 5 Framework. Now I provide the basic information about JUnit 5. The basics of JUnit 5 are,

  • Annotations
  • Test Life cycle methods
  • Assertions
  • Assumptions
  • Parameterized Test
  • Dynamic Tests
  • Tagging and Filtering

Maven Dependency for JUnit 5

To use JUnit 5 with Maven, you need to add the following dependencies to your pom.xml:

Gradle Dependency for JUnit 5

To use JUnit 5 with Gradle, you need to add the following dependencies to your build.gradle:

Annotations

The JUnit 5 framework uses different Annotations based on the test case design. Mostly in JUnit 5 @Test, @BeforeEach, @AfterEach, @BeforeAll, @AfterAll, @DisplayName, @Disabled these annotations are used. Basically, Annotations provides supplement information about the program in java. Annotations are always start with "@" symbol.

  • @Test: Marks a method as a test method.
  • @BeforeEach: Indicates that the annotated method should be executed before each test.
  • @AfterEach: Indicates that the annotated method should be executed after each test.
  • @BeforeAll: Indicates that the annotated method should be executed before all tests in the test class.
  • @AfterAll: Indicates that the annotated method should be executed after all tests in the test class.
  • @DisplayName: Provides a custom name for the test class or test method.
  • @Disabled: Disables the test method or class.

Test Life cycle methods

The JUnit 5 life cycle methods are annotated methods which are always start with @ symbol, these methods are executed at specific point in the test life cycle. The Life cycle methods are,

  • @BeforeEach, This Annotated method executes before each test method in the test class.
  • @AfterEach, This Annotated method executes after each test method in the test class, by sending signals to JUnit
  • @BeforeAll, this send signals to JUnit, Like the annotated method should be executed once before all test cased in the class.
  • @AfterAll, this annotation send signal to JUnit that is this annotated method should be run after all test cases are executed in the class.

Assertions

The JUnit 5 provides different methods in Assertions class for checking the expected Result. Assertions are used to check if a condition is true. If the condition is false, the test fails. Common assertions include:

  • assertEquals(expected, actual): Checks if two values are equal.
  • assertTrue(condition): Checks if a condition is true.
  • assertFalse(condition): Checks if a condition is false.
  • assertNotNull(object): Checks if an object is not null.

Example: Java program to demonstrate JUnit Assertion

For above code the test case passed successfully.

Assumptions

Assumptions in JUnit 5 provides a good way for checking the test cases conditionally based on preconditions and one more is if Assumptions is failed then it is marked as skipped rather then failed. Means Assumptions are used to when you want to skip a test then we use Assumptions in JUnit 5.

  • assumeTrue() this method is used to skip the test when a specified condition is not true for assumeTrue or not false for assumeFalse.
  • assumingThat() this method is allowing us, to execute a block of code based on a Boolean Assumptions, If the Assumptions is false the block is skipped.

Example: Java program to demonstrate JUnit Assumption

Parameterized Test

This Parameterized Test is used to test a Test case with different parameters for this we use @ParameterizedTest annotations.

Example: Java program to demonstrate parameterized test

Dynamic Tests

JUnit 5 introduces the concept of dynamic tests, which can be generated at runtime. These are created using the DynamicTest class.

For creating Dynamic Tests in Run time by using @TestFactory annotation. This TestFactory provides a feature to create dynamic test case in the run time of the Application.

Example: Java program to demonstrate Dynamic Test

Tagging and Filtering

We can tag our test cases by using @Tag annotation in the JUnit 5. Simply the Tags are labels for categorize the test cases. And Filter is used to filter and run the test cases by using the Tags.

Example: Java program to demonstrate Tagging and Filtering

@AfterAll, @AfterEach and @BeforeAll, @BeforeEach

Here we will understand one example for sum of two numbers by using such as @BeforeAll, @AfterAll, @BeforeEach, @AfterEach Annotations.

Running JUnit 5 Tests

JUnit 5 tests can be run in so many ways:

  • IDE Integration: Most modern IDEs like IntelliJ IDEA, Eclipse and NetBeans have built-in support for running JUnit 5 tests.
  • Build Tools: You can use build tools like Maven and Gradle to run JUnit 5 tests. Ensure you have the necessary dependencies configured in your pom.xml or build.gradle files.
  • Console Launcher: The JUnit Platform Console Launcher can be used to run tests from the command line.
Comment

Explore