EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials JUnit Tutorial JUnit Maven Dependency
Secondary Sidebar
JUnit Tutorial
  • Junit
    • JUnit 5 Assert
    • JUnit RunWith
    • JUnit AssertThat
    • JUnit Report
    • JUnit BeforeClass
    • JUnit Framework
    • JUnit Data Provider
    • JUnit Runner
    • JUnit 5 Parameterized Tests
    • JUnit BeforeAll
    • JUnit Private Methods
    • JUnit Integration Test
    • JUnit Maven Dependency
    • JUnit Annotations?
    • JUnit Testing
    • JUnit Test Order
    • JUnit 5 RunWith
    • JUnit 4 Maven
    • JUnit Jar
    • JUnit assertEquals
    • JUnit 5 Parameterized Test
    • JUnit Dependency
    • JUnit Fail
    • JUnit Disable Test
    • JUnit Assert
    • JUnit in Maven
    • JUnit 5 Gradle
    • JUnit XML
    • JUnit XML Format
    • JUnit Eclipse
    • JUnit Test Suite
    • JUnit Parameterized Test
    • JUnit assert exception
    • JUnit Code Coverage
    • JUnit Jupiter
    • JUnit Rule
    • JUnit version
    • Junit Interview Questions

JUnit Maven Dependency

By Payal UdhaniPayal Udhani

JUnit Maven Dependency

Introduction to JUnit Maven Dependency

JUnit is the testing framework that is extensively used for java projects built in the maven project format for unit testing purposes. Here we will see how we can mention the JUnit dependency in pom.xml file in a maven project and see various annotations and assert methods that can be used in java projects. Two types of JUnit versions were written from scratch and have huge differences in their usage. We will see about both of them and how we can perform unit testing in eclipse IDE for java projects.

We require org.junit package while performing unit testing with JUnit 4 and org.junit.jupiter.api package while doing testing with JUnit 5.

Tags for JUnit Maven Dependency

The latest release dependency tags for JUnit4 and JUnit5 are as follows:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,328 ratings)

Tag for JUnit4

Following is the latest release dependency tag for JUnit 4.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Code:

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>

Tag for JUnit5

JUnit 5 has the following dependency tag to be added in maven’s pom.xml file for performing unit testing in java projects using maven.

Code:

<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>

Examples of JUnit Maven Dependency

Given below are the examples mentioned:

Example #1: JUnit 4 

Begin by creating a new maven project.

JUnit Maven Dependency 1

Consider the following example where we have two methods, one for calculating product and other for division in class named Arithmetic. Create a new java class file named Arithmetic.

Code:

public class Arithmetic {
public int product(int number1, int number2) {
return number1 * number2;
}
public int division(int number1, int number2) {
return number1 / number2;
}
}

This class file should be structured and located in the following way:

class file should be structured and located

Now, we will create a test class named ArithmeticTest according to coding conventions the name of the testing class should be the name of the class which is being tested followed by the Test keyword. This will contain test methods whose naming convention is the name of the method being test prepended with test word. Hence, here are two testing methods named testProduct and testDivision.

The test class can be created by clicking on new -> other and then the test case option inside JUnit as shown below:

test class can be created by clicking on new ->

Then select the methods you want to add in the test class file and mention the name of the test class file and the file being tested in the following window to create a test class file.

select the methods you want to add in the test class file

Code:

import org.junit.*;
public class ArithmeticTest {
@BeforeClass
public static void setUpClass() {
// This block is executed before all the methods of test
}
@Before
public void setUp() {
// This block is executed before each method of test
}
@Test
public void testProduct() {
// This block is executed for testing product method of Arithmetic class
Arithmetic arithmetic = new Arithmetic();
int number1 = 100;
int number2 = 5;
int actualvalue = arithmetic.product(number1, number2);
int expectedvalue = 500;
assertEquals(expectedvalue, actualvalue);
}
@Test
public void testDivision() {
// This block is executed for testing division method of Arithmetic class
}
@After
public void tearDown() {
// This block is executed after each method of test
}
@AfterClass
public static void tearDownClass() {
// This block is executed after all the methods of test
}
}

Note: All the methods above that are annotated with BeforeClass, Before, After and AfterClass are optional. Only test methods are required in test classes. Each method has its specific purpose and the time when it is executed. Mentioned in the comment section of each method.
  • BeforeClass: The code that you want to execute only once before any other method is executed such as setting up an environment for testing, opening database connection, etc can be included over here.
  • Before: This is the method that will be executed before each of the test methods is executed.
  • Test: The code where you want to check the execution and performance of your method can be included in the test method. Many assertion methods are available that will be useful to decide the correctness of working. They will result in java.lang.AssertionError is condition fails.
  • After: This is the method that will be executed after each of the test methods is executed.
  • AfterClass: The code that you want to execute only once after all other test methods are executed such as clearing up the environment of testing, closing database connection, etc can be included over here.

This ArithmeticTest class file should be structured and located in the following way:

JUnit Maven Dependency 5

Maven’s pom.xml will contain:

Code:

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.educba</groupId>
<artifactId>JUnit-testing-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

This file should be structured and present in the following way:

JUnit Maven Dependency 6

To check the test cases and execute the test, open the outline window from the window -> preferences -> outline and then right-click on any method of test class file you want to execute and run as the JUnit test option. After running the above testProduct method of ArithmeticTest class you will get the following output on Junit console.

JUnit Maven Dependency 7

The green bar shows that the test case is successfully executed and there are no errors. For running whole class file of test cases you can right-click the ArithmeticClass file and run a Junit test that gives following output.

JUnit Maven Dependency 8

Example #2: JUnit 5

Considering the same above example for Junit 5 dependency, our Arithmetic class file will remain same. Our pom.xml will be somewhat like the following.

Code:

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.educba</groupId>
<artifactId>JUnit-testing-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
</dependencies>
</project>

ArithmeticTest will will be as follows:

Code:

package com.educba;
import org.junit.gen5.api.AfterAll;
import org.junit.gen5.api.AfterEach;
import org.junit.gen5.api.BeforeAll;
import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.api.Test;
import staticorg.junit.gen5.api.Assertions.assertEquals;
public class ArithmeticTest {
@BeforeAll
public static void setUpClass() {
// code executed before all test methods
}
@BeforeEach
public void setUp() {
// code executed before each test method
}
@Test
public void testProduct() {
Arithmetic arithmetic = new Arithmetic();
int number1 = 100;
int number2 = 5;
int actualvalue = arithmetic.product(number1, number2);
int expectedvalue = 400;
assertEquals(expectedvalue, actualvalue);
}
@Test
public void test Division() {
// test method
}
@AfterEach
public void tearDown() {
// code executed after each test method
}
@AfterAll
public static void tearDownClass() {
// code executed after all test methods
}
}

Running the testProduct test gives the following output in the above test file of Junit 5 I had changed the expected value to 400 that is not equal to 100 * 5 that is 500 hence a red bar with error is thrown. Note that the names of annotations are changed in Junit 5 testing however their functionality remains the same and unaffected.

JUnit Maven Dependency 9

If we change the testProduct method’s expected value back to 500 it will give the green bar and successfully execute the test case.

will give the green bar and successfully execute the test case.

Just make sure that while creating a test file for JUnit 5, you choose the option correctly on the window where the name and other details are asked while creating it.

you choose the option correctly on the window

Recommended Articles

This is a guide to JUnit Maven Dependency. Here we discuss the introduction of JUnit Maven Dependency along with examples. We can add JUnit 4 or JUnit 5 maven dependency in our pom.xml to perform unit testing in java projects that are structured and use maven in it. You may also have a look at the following articles to learn more –

  1. JUnit Annotations 
  2. Maven Plugins
  3. Maven Shade Plugin
  4. Jetty Maven plugin
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more