JUnit - Writing Sample Test Cases for CutOffMarkCalculation Java Service
Last Updated :
23 Jul, 2025
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. Automated testing is more helpful. In this article, how to write JUnit for the cutoff marks calculation based on caste and marks. Let us take a simpler service namely "CutOffMarkCalculatorService" which has a business logic to check eligibility based on caste and marks. And also a simpler "TestCutOffMarkCalculatorService" that uses this service and prepares positive and negative test scenarios. The whole thing is integrated as a maven project.
Project Structure:
This is a maven project
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.cutOffMarkCalculator</groupId>
<artifactId>CutOffMarkCalculatorService</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.cutOffMarkCalculator.*CutOffMarkCalculator*</param>
</targetClasses>
<targetTests>
<param>com.gfg.cutOffMarkCalculator.*</param>
</targetTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
Let us see the key java files
Service class
CutOffMarkCalculatorService.java
Java
public class CutOffMarkCalculatorService {
// While checking the marks, we have to
// check whether is it positive or not
public void isValidMarks(int cutOffMarks) {
if (cutOffMarks < 0) {
throw new IllegalArgumentException("Given cutOffMarks should be positive!");
}
}
public boolean isEligibleByCaste(int cutOffMarks, String caste) {
isValidMarks(cutOffMarks);
if (caste != null && caste.equalsIgnoreCase("FC") && cutOffMarks >= 600) {
return true;
} else if (caste != null && caste.equalsIgnoreCase("BC") && cutOffMarks >= 580) {
return true;
} else if (caste != null && caste.equalsIgnoreCase("MBC") && cutOffMarks >= 550) {
return true;
} else if (caste != null && caste.equalsIgnoreCase("SC") && cutOffMarks >= 400) {
return true;
} else if (caste != null && caste.equalsIgnoreCase("ST") && cutOffMarks >= 350) {
return true;
} else {
return false;
}
}
}
Test class
TestCutOffMarkCalculatorService.java
Java
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TestCutOffMarkCalculatorService {
@DisplayName("Test check eligibility by caste-Positive")
@Test
public void testCheckEligibilityByCaste() {
CutOffMarkCalculatorService marksObject = new CutOffMarkCalculatorService();
assertEquals(true, marksObject.isEligibleByCaste(700, "FC"));
assertEquals(true, marksObject.isEligibleByCaste(600, "BC"));
assertEquals(true, marksObject.isEligibleByCaste(550, "MBC"));
assertEquals(true, marksObject.isEligibleByCaste(420, "SC"));
assertEquals(true, marksObject.isEligibleByCaste(370, "ST"));
Assertions.assertThrows(IllegalArgumentException.class, () -> {
marksObject.isValidMarks(-1);
});
}
@DisplayName("Test check eligibility by caste-Negative")
@Test
public void testCheckEligibilityByCasteNegative() {
CutOffMarkCalculatorService marksObject = new CutOffMarkCalculatorService();
assertEquals(false, marksObject.isEligibleByCaste(300, "FC"));
assertEquals(false, marksObject.isEligibleByCaste(400, "BC"));
assertEquals(false, marksObject.isEligibleByCaste(350, "MBC"));
assertEquals(false, marksObject.isEligibleByCaste(250, "SC"));
assertEquals(false, marksObject.isEligibleByCaste(200, "ST"));
Assertions.assertThrows(IllegalArgumentException.class, () -> {
marksObject.isValidMarks(-1);
});
}
}
JUnit Execution:
Output:
In case of any errors, JUNIT testing will help us to indicate in the below way
Conclusion
In order to improve the quality of software and to provide error-free software, JUnit testing is mandatory and all software, industries use these JUnit techniques. Business logic is also enhanced in this way.
Similar Reads
JUnit - Writing Sample Test Cases for StudentService in Java In many parts of projects, collections play a major role. Among that ArrayList is a user-friendly collection and many times it will be required to develop software. Let us take a sample project that involves a "Student" model that contains attributes such as studentId, studentName, courseName, and G
5 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
Writing Templates for Test Cases Using JUnit 5 Writing consistent and reusable test case templates is essential for maintaining quality and ensuring uniformity in large-scale projects. Test case templates provide a structured approach for documenting and executing test scenarios, making it easier to identify issues, automate testing, and validat
7 min read
Run JUnit Test Cases From the Command Line JUnit allows us to test individual pieces of code by creating test cases, typically for methods and classes. Running tests via the command line can be beneficial for CI/CD pipeline scripts or when the environment does not support an IDE.JUnit supports integration with several build tools, such as Ma
5 min read
Running JUnit Tests Programmatically, from a Java Application JUnit is a popular testing framework in the Java ecosystem that enables developers to create and run tests efficiently. While it is common to run tests through IDEs or build tools, running them programmatically allows greater flexibility in integrating testing into various stages of the application
5 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