JUnit 5 – Maven Dependency
Last Updated :
27 Dec, 2023
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:
- Java version 8 (at least)
- Basic Knowledge of Dependency management and Maven build management tool.
- Java Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA, or Visual Studio Code.
Note: JUnit 5 requires at least Java 8 version.
Required Maven Dependencies for JUnit 5
- junit-jupiter-api
- junit-jupiter-engine
- junit-platform-suite
- junit-jupiter-params
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
- Go to the `file` menu and click new and navigate to `Spring Starters project`. If you don’t find the Spring Starters project immediately after `new`, then click other and find `Spring Starters project`.
- File > new > Spring Starters project.
- Name your project and configure the default options given if necessary.
Recommended requirements:
Project type: Maven (Mandatory)
Packaging: Jar
Java Version: 8
Language: Java
- Make sure that, you have chosen the type as ‘Maven‘ and the version of Java should be at least 8.
- Add dependencies If you need any, otherwise, click `finish`.
Step 2: Adding dependencies
Let us configure our `pom.xml` file with the following dependencies:
JUnit Jupiter API
- The JUnit Jupiter API is the part of JUnit 5 framework which provides annotations, assertions, and other features for defining and running test cases and serves as a programming model for writing tests in Java. To add the JUnit Jupiter API in pom.xml file, copy and paste the following code.
XML
< 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
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.
- Ensure that you have choose `New JUnit Jupiter test` and click `finish`.
Example of Testcase1:
Test Script: Let us write the first test case i.e. `Testcase1`.
Java
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:
Share your thoughts in the comments
Please Login to comment...