Updated February 20, 2023
Introduction to JUnit Dependency
JUnit is an API dependency used to provide the public API; with the help of this API, we can write the test with the extension of JUnit 5. In other words, we can say that JUnit dependency provides the correct path to execute a test case that uses JUnit 5. JUnit is an open-source framework used to test applications that are based on java. JUnit is a simple framework to execute the repeatable test cases per the requirement. In other words, we can say that it is an instance of the xUnit and used to perform the unit testing on the application.
What is JUnit Dependency?
The stage is liable for sending off testing systems on the JVM. It characterizes a steady and strong point of interaction among JUnit and its clients, for example, fabricating apparatuses. The stage effectively coordinates clients with JUnit to find and execute tests.
It likewise characterizes the TestEngine API for fostering a testing system that suddenly spikes demand for the JUnit stage. By executing a custom TestEngine, we can plug outsider testing libraries into JUnit.
We can get the required conditions according to the prerequisite by adding the JUnit-Jupiter reliance to the test scope. This is an aggregator antique that improves on the reliance on the board since it has the accompanying transitive conditions:
The JUnit-Jupiter-programming interface reliance (assemble scope) gives the public API to composing tests and augmentations.
The JUnit-Jupiter-params reliance (aggregate degree) offers help for composing defined tests.
The JUnit-Jupiter-motor reliance (runtime scope) contains the execution of the JUnit Jupiter test motor that runs our unit tests. Assuming that we add this reliance to our classpath, the Maven Surefire and Failsafe modules (form 2.22.0 or fresher) can run tests that use JUnit 5.
How to Add JUnit Dependency?
Now let’s see how we can add JUnit dependency as follows.
There are two ways to add the dependency to the project as follows.
Firstly, we can add the jar files inside the java project by using the following steps.
- First, we need to create a maven project in eclipse.
- Click on the properties of the project.
- After that, we need to click on the Java build path.
- Click on the Libraries tab from the pop-up window.
- Click on the Add Library button.
- Select the JUnit library from the local machine.
- Click on the next and apply button.
- Finally, click on the Save button.
In a second way, we need to create the maven project, and here, we can add JUnit dependency inside the pom.xml file as follows.
<dependency>
<groupId>org.JUnit.jupiter</groupId>
<artifactId>JUnit-jupiter-engine</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.JUnit.platform</groupId>
<artifactId>JUnit-platform-runner</artifactId>
<version>${JUnit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.JUnit.platform</groupId>
<artifactId>JUnit-platform-launcher</artifactId>
<version>${JUnit.platform.version}</version>
<scope>test</scope>
</dependency>
But the above expert conditions designs might throw a mistake like Missing ancient rarity org.JUnit.platform: JUnit-stage runner:jar: 5.4.0. You can see the blunder toward the start of the < dependency> XML component, and when you mouse over the mistake, it will show the blunder message.
The mistake implies there is no curio org.JUnit.platform:JUnit-stage runner:jar: 5.4.0 in the focal expert vault. So you should add the current JUnit reliance container record rendition for the reliance.
Presently you can follow segment 2.1 to add JUnit independent library container records into your obscure task, then, at that point, find the connected container variant under the venture name/JUnit 5 subfolder.
JUnit Dependency Remove
First, we must run the dependency tree command on the command line.
mvn dependency:tree
We will get all duplicate dependencies that are present inside the pom.xml file. Here, we get a warning about copy conditions in pom.xml. We also notice that rendition 3.11 of center lang3.jar is added to the task, despite a higher variant, 3.12.0, is available. It happened because Maven picked the reliance that showed up later in the pom.xml, as shown in the following screenshot.
Now we need to analyze duplicate commands on terminal
mvn dependency:analyze – duplicate
The final result of the above command is shown in the following screenshot.
Example Projects
Now let’s see an example of JUnit dependency for better understanding.
Here we create the maven project, and inside the pom.xml file, we add all required JUnit dependencies.
Just create a class file like a sample demo and paste the following code.
package com.demo;
import static org.JUnit.Assert.assertTrue;
import org.JUnit.After;
import org.JUnit.AfterClass;
import org.JUnit.Before;
import org.JUnit.BeforeClass;
import org.JUnit.Test;
public class sampledemo {
@BeforeClass
public static void beforeClassmethod() {
System.out.println("Hi welcome in Before Class Method");
}
@Before
public void beforemethod() {
System.out.println("Before Test Case strategy ");
}
@Test
public void Test() {
System.out.println("Welcome in First Test");
}
@After
public void aftermethod() {
System.out.println("Hi welcome in After Test Case");
}
@AfterClass
public static void afterClassmethod() {
System.out.println("Hi welcome in After Class");
}
}
Explanation
In the above example, we try to cover JUnit annotation as shown in the above code. We illustrated the final output of the above implementation using the following screenshot.
In another example, we can create a simple java project and add the entire external jar we require. Now let’s see the implementation as follows.
First, we must create a simple java class and write the following code.
package com.datap;
import static org.JUnit.Assert.assertEquals;
import org.testng.annotations.Test;
public class sample {
@Test
public void stringcomparison() {
String x = "ASasasasss12323SASJASAS ";
assertEquals("ASasasasss12323SASJASAS ",x);
}
}
Explanation
We write a simple test case to match the string using assertEquals, as shown in the above code.
We need to write the TestRunner class to execute the test case as follows.
import org.JUnit.runner.JUnitCore;
import org.JUnit.runner.Result;
import org.JUnit.runner.notification.Failure;
public class TRunner {
public static void main(String[] args) {
Output output= JUnitCore.runClasses(TestJUnit.class);
for (Failure failure : output.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(output.wasSuccessful());
}
}
Explanation
We write the test runner class to verify the result in the above code. We illustrated the final output of the above class by using the following screenshot.
Conclusion
We hope from this article you learn more about JUnit dependency. From the above article, we have taken in the essential idea of the JUnit dependency and see the representation and example of the JUnit dependency. Furthermore, this article taught us how and when to use the JUnit dependency.
Recommended Articles
This is a guide to the JUnit Dependency. Here we discuss the definition and how to add JUnit dependency along with an example project. You may also have a look at the following articles to learn more –