Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaJUnit – Writing Sample Test Cases for CutOffMarkCalculation Java Service

JUnit – Writing Sample Test Cases for CutOffMarkCalculation Java Service

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:

Project Structure

 

This is a maven project

pom.xml 

XML




<?xml version="1.0" encoding="UTF-8"?>
         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.gfg.cutOffMarkCalculator</groupId>
   <artifactId>CutOffMarkCalculatorService</artifactId>
   <packaging>jar</packaging>
   <version>1.0-SNAPSHOT</version>
   <properties>
      <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:

JUnit Execution

 

Output:

Success Output

 

In case of any errors, JUNIT testing will help us to indicate in the below way

Failure Output

 

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.

RELATED ARTICLES

Most Popular

Recent Comments