Author

Author- Ram Ranjeet Kumar

Tuesday, August 15, 2023

Write Junit Test case for a Java class with all functionality



To use JUnit in your Maven project, you need to add the JUnit dependency to your pom.xml file. Additionally, if you're using JUnit 5 (JUnit Jupiter), you'll also need to include the JUnit Jupiter API and the JUnit Jupiter Engine dependencies. Here's how you can do it:

<dependencies>

    <!-- JUnit 5 -->

    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-api</artifactId>

        <version>5.8.1</version>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-engine</artifactId>

        <version>5.8.1</version>

        <scope>test</scope>

    </dependency>

</dependencies>


 Here's the Calculator class

public class Calculator {


    public int add(int a, int b) {

        return a + b;

    }


    public int subtract(int a, int b) {

        return a - b;

    }


    public int multiply(int a, int b) {

        return a * b;

    }


    public double divide(int a, int b) {

        if (b == 0) {

            throw new ArithmeticException("Cannot divide by zero");

        }

        return (double) a / b;

    }

}


This is the Calculator class that performs basic arithmetic operations. Make sure both the Calculator class and the JUnit test class are in the same package or properly imported for the test cases to work as intended.

Here's how you can write JUnit test cases to test its functionality:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {

    @Test
    public void testAddition() {
        Calculator calculator = new Calculator();
        int result = calculator.add(5, 7);
        assertEquals(12, result);
    }

    @Test
    public void testSubtraction() {
        Calculator calculator = new Calculator();
        int result = calculator.subtract(10, 3);
        assertEquals(7, result);
    }

    @Test
    public void testMultiplication() {
        Calculator calculator = new Calculator();
        int result = calculator.multiply(4, 6);
        assertEquals(24, result);
    }

    @Test
    public void testDivision() {
        Calculator calculator = new Calculator();
        double result = calculator.divide(15, 3);
        assertEquals(5.0, result, 0.001); // Using delta for double comparison
    }

    @Test
    public void testDivisionByZero() {
        Calculator calculator = new Calculator();
        assertThrows(ArithmeticException.class, () -> calculator.divide(10, 0));
    }
}


In this example, the Calculator class has methods for addition, subtraction, multiplication, and division. The JUnit test class CalculatorTest contains several test methods:
  • testAddition: Tests the addition method.
  • testSubtraction: Tests the subtraction method.
  • testMultiplication: Tests the multiplication method.
  • testDivision: Tests the division method.
  • testDivisionByZero: Tests whether division by zero throws an ArithmeticException.
Remember to have your Calculator class and JUnit library properly set up in your project for these test cases to work. Additionally, you might need to adjust the code based on your specific project structure and requirements.

No comments:

Post a Comment