简体   繁体   English

JUnit 测试:如何使用 JUnit 4 为程序编写参数化测试用例以查找圆的面积?

[英]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.问题是我对程序的输入是整数数据类型,而输出是双精度数据类型。因此,在编写参数化的 JUnit 测试用例时,assertEquals 会自动取消……显示 2 个错误。 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)它们如下: - Assert 类型中的方法 assertEquals(double, double) 已弃用 - Circle 类型中的方法 findarea(int) 不适用于参数 (int, double)
Here the method findarea is my method to find the area of the circle.这里findarea方法是我找到圆的面积的方法。 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.当我尝试运行测试用例时,它显示以下错误: 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!请帮我重写这个JUnit测试用例。我已经尝试了几次。请帮助我。提前谢谢!

To fix the error you are getting, provide delta as the third parameter in assertEquals.要修复您遇到的错误,请提供 delta 作为 assertEquals 中的第三个参数。

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

Delta is basically the amount of precision. Delta基本上是精度。 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 . Math.abs(expected - actual) <= delta Eg例如

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.请注意, assertEquals(double, double)现在已弃用。 More details here更多细节在这里
Why is assertEquals(double,double) deprecated in JUnit? 为什么在 JUnit 中不推荐使用 assertEquals(double,double)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM