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
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING