Search code examples
javaunit-testingjunit4parameterizedparameterized-unit-test

JUnit Testing:How to write parameterized test cases for a program to find the area of a circle using JUnit 4?


The problem is my input to the program is of integer data type and the output is of double data type.So,while writing parameterized JUnit test cases,assertEquals gets striked off automatically...showing 2 errors. They are as follows: -The method assertEquals(double, double) from the type Assert is deprecated -The method findarea(int) in the type Circle is not applicable for the arguments (int, double)
Here the method findarea is my method to find the area of the circle. Here is my code.

package BasicTesting;
    
    import java.io.*;
    class Circle
      {
        public static void main(String args[])throws IOException  {
        BufferedReader ob = new BufferedReader(new InputStreamReader(System.in));
        int radius ;
            
        System.out.println("Enter the radius of circle.. ");
        radius=Integer.parseInt(ob.readLine());
        Circle a=new Circle();
        double g=a.findarea(radius);
        System.out.println(g);
        }
        
         
     
    
        public static double findarea(int radius){
            double area;
            
            
            double pi=3.14;
            area = pi * radius * radius;
               return area;
            }
            }

This is my test case

package BasicTesting;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)

public class CircleTest {
    
    
    
    public CircleTest(int input, double expected,double delta) {
        super();
        this.input = input;
        this.expected = expected;
        this.delta = delta;
    }

    Circle ob=new Circle();
    private int input;
    private double expected;
    private double delta;
    @Parameters
    
    public static Iterable<Object[]> testConditions() {
        
        return Arrays.asList(new Object[][] {{1,3.14d,-2.14d},{2,12.56d,-10.56}});
        

        
    
    }

    

    

    @Test
    public void Circletest() {
        assertEquals(expected,Circle.findarea(input));
        
        
    }

}

    

When I tried to run the test case,it shows the following error: java.lang.AssertionError: Use assertEquals(expected, actual, delta) to compare floating-point numbers. Please help me to rewrite this JUnit test case.I have tried several times.Please help me out.Thanks in adavance!


Solution

  • To fix the error you are getting, provide delta as the third parameter in assertEquals.

    @Test
    public void circleTest() {
        assertEquals(expected, Circle.findarea(input), delta);
    }
    

    Delta is basically the amount of precision. This means if the absolute diff between expected and actual value is less or equal to the delta. It will still consider a match. In short

    Math.abs(expected - actual) <= delta. E.g.

    assertEquals(1.1, 1.2, 0.1); // diff: 0.1, result : true 
    assertEquals(1.10, 1.20, 0.09); // diff: 0.1, result : false 
    assertEquals(1.9999, 2.0, 0.10); // diff: 0.0001, result:  true 
    

    Note, assertEquals(double, double) is deprecated now. More details here
    Why is assertEquals(double,double) deprecated in JUnit?