Open In App

JUnit 5 – Maven Dependency

JUnit 5 is a widely used testing framework in the Java ecosystem. It is the successor of JUnit 4 and is designed to address its limitations. JUnit framework allows the developers to write and run the tests for their Java code. These tests help ensure that the code functions correctly and continues to work as expected as changes are made.

Using Maven Dependency in JUnit 5

Maven is a popular build management tool in the Spring Boot framework. Instead of the Gradle build management tool, the Maven dependency management tool allows us to configure the pom.xml file.



Prerequisites to the Topic

To understand this, we need to have some prerequisites. They are:

Note: JUnit 5 requires at least Java 8 version.



Required Maven Dependencies for JUnit 5

Note: Here, we will be using junit-jupiter-api Maven dependency

IDE Setup JUnit 5 Maven Dependency

Here, we use Eclipse IDE for Java and Web Developers 2023-06. You may also use other platforms like IntelliJ, Spring suite tool, Spring Initializer, etc.

Step By Step Implementation of the Application

Step 1: Creation of Project

Recommended requirements:

Project type: Maven (Mandatory)
Packaging: Jar
Java Version: 8
Language: Java

Step 2: Adding dependencies

Let us configure our `pom.xml` file with the following dependencies:

JUnit Jupiter API




<dependency>
     <groupId>org.junit.jupiter</groupId>
     <artifactId>junit-jupiter-api</artifactId>
     <version>5.8.0</version>
     <scope>test</scope>
</dependency>

Project Setup for the Application

Now, the project setup is ready, let us look into an example to understand how to write and run the test cases of JUnit using the Maven tool.

Step 1: Create a new package in the `src/test/java`, right-click on `src/test/java` > new > package.

After Creating Package:

Step 2: Create a class inside the package and name the class. i.e. `Addition`. To create a class, right-click on package > new > class

After Creating Class:

Step 3: Write the logic which you want to test in `Addition.java`.




//Java program to demonstrate a simple addition operation
package project;
  
public class Addition {
    public int sum(int a, int b) {
        return a+b;
    }
}

Step 4: Now, create a test case inside the package by right click on the package > new > others > java > JUnit > JUnit test case.

Example of Testcase1:

Test Script: Let us write the first test case i.e. `Testcase1`.




package project;
  
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
  
class Testcase1 {
  
       @Test
       public void test1() {
        Addition addition = new Addition();
        int actual = addition.sum(2, 3);
        int expected = 5;
        assertEquals(actual, expected);
        System.out.println("Test executed");
  
    }
}

Explanation of the Program:

The above code snippet is the basic script of a test case. In the class `Testcase1`, we defined a method named `test1` which is annotated with `@Test` representing it is a test method. We have created an `addition` object for `Addition.java` and calculated the sum of two numbers and stored it in `actual` variable and asserting it with `expected` value using `assertEquals()` method.

Steps to run the Application

To run the application, go to project explorer > right-click on your project > Run as > Spring Boot App.

Output of Application

JUnit View:

Console View:


Article Tags :