JUnit Testcases Preparation For a Maven Project
Last Updated :
23 Jul, 2025
The quality of a software project is the desired aspect always. Programmers write code and they will do unit testing, block level testing, and sometimes integration testing too. But they will provide a few examples alone for the use cases. For example, if they are finding a number is prime or not, maybe they will provide some 2 to 3 different sets of numbers and confirm that the written code is working fine. But in practice, it is not the usual case. To overcome the above flaws, automated testing is required and it can be done via JUnit. In this tutorial let us see how to do that.
Example Project
Project Structure:
This is a maven-driven project. Hence let us see
pom.xml
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gfg.examples</groupId>
<artifactId>NumberService</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- https://maven.apache.org/general.html#encoding-warning -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>5.3.1</junit.version>
<pitest.version>1.4.3</pitest.version>
</properties>
<dependencies>
<!-- junit 5, unit test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>maven-mutation-testing</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pitest.version}</version>
<executions>
<execution>
<id>pit-report</id>
<phase>test</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
</execution>
</executions>
<!-- https://github.com/hcoles/pitest/issues/284 -->
<!-- Need this to support JUnit 5 -->
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>0.8</version>
</dependency>
</dependencies>
<configuration>
<targetClasses>
<param>com.gfg.examples.*Calculator*</param>
<param>com.gfg.examples.*Stock*</param>
</targetClasses>
<targetTests>
<param>com.gfg.examples.*</param>
</targetTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now let us see the business logic file
CalculatorService.java
Java
public class CalculatorService {
public boolean checkPositive(int inputNumber) {
boolean isPositive = false;
// Lets include 0 as it is
// neither positive nor negative
if (inputNumber >= 0) {
isPositive = true;
}
return isPositive;
}
public boolean checkNegative(int inputNumber) {
boolean isNegative = false;
// Lets include 0 as it is
// neither positive nor negative
if (inputNumber <= 0) {
isNegative = true;
}
return isNegative;
}
public boolean checkEvenNumber(int inputNumber) {
boolean isEven = false;
if (Math.abs(inputNumber) % 2 == 0) {
isEven = true;
}
return isEven;
}
public boolean checkOddNumber(int inputNumber) {
boolean isOdd = false;
if (Math.abs(inputNumber) % 2 == 1) {
isOdd = true;
}
return isOdd;
}
// Similarly we can write so many methods to check
// whether is it divisible by 6, 9 etc.,
public boolean checkWhetherPrimeOrNot(int inputNumber) {
boolean isPrime = true;
int checkingNumber = Math.abs(inputNumber) / 2;
for (int idx = 2; idx <= checkingNumber; idx++) {
if (inputNumber % idx == 0) {
isPrime = false;
// Once we get the satisfying condition,
// we need to come out of the loop
break;
}
}
return isPrime;
}
}
TestCalculatorService.java
Java
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class TestCalculatorService {
@Test
public void testForPositiveNumber() {
CalculatorService calculatorService = new CalculatorService();
assertEquals(true, calculatorService.checkPositive(10000));
assertNotEquals(true, calculatorService.checkPositive(-10001));
// kill mutation #1
assertEquals(true, calculatorService.checkPositive(0));
}
@Test
public void testForNegativeNumber() {
CalculatorService calculatorService = new CalculatorService();
assertEquals(true, calculatorService.checkNegative(-210000));
assertNotEquals(true, calculatorService.checkNegative(210009));
// kill mutation #1
assertEquals(true, calculatorService.checkNegative(0));
}
@Test
public void testForEvenNumber() {
CalculatorService calculatorService = new CalculatorService();
assertEquals(true, calculatorService.checkEvenNumber(410000));
assertEquals(true, calculatorService.checkEvenNumber(-221144));
assertNotEquals(true, calculatorService.checkEvenNumber(410001));
assertNotEquals(true, calculatorService.checkEvenNumber(-221149));
}
@Test
public void testForOddNumber() {
CalculatorService calculatorService = new CalculatorService();
assertEquals(true, calculatorService.checkOddNumber(12345));
assertEquals(true, calculatorService.checkOddNumber(-232323));
assertNotEquals(true, calculatorService.checkOddNumber(12348));
assertNotEquals(true, calculatorService.checkOddNumber(-232316));
}
@Test
public void testForPrimeNumber() {
CalculatorService calculatorService = new CalculatorService();
// 12345 is not prime
assertEquals(false, calculatorService.checkWhetherPrimeOrNot(12345));
// 232323 is not prime
assertEquals(false, calculatorService.checkWhetherPrimeOrNot(-232323));
// 9839 is prime
assertEquals(true, calculatorService.checkWhetherPrimeOrNot(-9839));
}
}
We can write our required business logic in the first java file (CalculatorService.java) and similarly its relevant JUnit test case checking in the second java file (TestCalculatorService.java). Always it is good to check for multiple scenarios in the test file. Then only our business logic will get stronger and when it is deployed in production, i.e. during the usage of the application, there are no issues observed. Hence always keep JUnit testing mandatory in any software project. As we have provided the dependency in pom.xml for JUNIT, they are all downloaded from maven central. We will get the option to run the file as a JUNIT test as shown below
Output:
In case there are any errors encountered, we will be getting errors as indicated below. Suppose, our code is as follows, then the last statement is incorrectly written. So either the business logic written way is wrong, or wrongly tested. On execution of the test case, we will come up with this error.
Java
@Test
public void testForPrimeNumber() {
CalculatorService calculatorService = new CalculatorService();
// 12345 is not prime
assertEquals(false, calculatorService.checkWhetherPrimeOrNot(12345));
// 232323 is not prime
assertEquals(false, calculatorService.checkWhetherPrimeOrNot(-232323));
// 9839 is prime, but we have written to get it as not prime.
// Check for input number or else check for the assert part how it is
// written
assertEquals(false, calculatorService.checkWhetherPrimeOrNot(-9839));
}
Conclusion
Hence writing JUnit provides quality software and it is a good asset to the software industry.
Similar Reads
JUnit - Testcases for Credit Card Validation as a Maven Project In this article let us see a much-required functionality that is credit card validation as well as its relevant JUnit test cases for given credit card numbers. In an e-commerce project, credit card payment is the valid and much desired required functionality. We need to have a proper validation mech
6 min read
JUnit - Inventory Stock Testing as a Maven Project The quality of the software is very important. Though Unit tests and integration tests are done in the manual testing way, we cannot expect all kinds of scenarios to test. Hence as a testing mechanism, we can test a software code or project by means of JUnit. In this article let us see how to do tes
4 min read
JUnit for Armstrong, Unique and Perfect Numbers as a Maven Project The quality of software can be enhanced by writing automated testing. JUnit plays a significant role in testing the software. In this article, let us see a few samples of magic numbers like Armstrong, Unique, and Perfect Numbers and their corresponding JUnit as maven project. Maven Example Project S
4 min read
JUnit Testing For MySQL Project in Java For testing a software project, automated testing is always good and that will produce error-prone results. In the case of manual testing, there are possibilities of human errors. In this article let us take a sample project and let us see how to write JUnit test cases for it. Example Project Projec
6 min read
How to Test a Maven Project using EasyMock? Always a software project is prepared and tested as an individual unit. We pass different scenarios and expect the original values should match. One such approach is "Easymock" and in this article via a sample project, handled when and how to mock void methods. We are going to see this approach via
3 min read
JUnit - Sample Test Cases for String Java Service Always automated testing helps to overcome various bugs. It will help in the integrity of the software as well. Here in this article, let us see how to write JUnit test cases for various String-related functions like "Isogram, Panagram, Anagram" etc., In the entire lifecycle of a software project, w
5 min read