为什么我的assertEquals不起作用?

时间:2022-08-23 14:44:24

I am testing one of my methods, double getSqrt(double s), and having the following JUnit test code:

我正在测试我的一个方法,双getSqrt(double s),并具有以下JUnit测试代码:

@Test
public void testGetSqrt() {
    System.out.println("getSqrt");
    double s = 16.0;
    Calculator instance = new Calculator();
    double expResult = 4.0;
    double result = 4.000000000052429; // was "instance.getSqrt(s);" here
                                       // now hardcode for test purpose
    System.out.println(System.getProperty("java.class.path"));
    assertEquals(expResult, result, 0.0);
}

It failed every time, no matter how I changed the tolerance "0.0", "0.1", "0.00", "0.01", "0.001".... The report is the following:

无论我如何改变公差“0.0”,“0.1”,“0.00”,“0.01”,“0.001”......它每次都失败了....报告如下:

AssertionFailedError: expected:<4.0>; but was:<4.000000000052429>

AssertionFailedError:expected:<4.0>;但是:<4.000000000052429>

What I am using is JUnit 4.12 and Hamcrest 1.3.

我使用的是JUnit 4.12和Hamcrest 1.3。

And my imports:

我的进口:

import org.junit.Test;
import static org.junit.Assert.*;

Did I use the tolerance wrong?

我使用了容差吗?

Output on the console:

控制台上的输出:

compile-test-single:
Testsuite: descriptivestatisticsapplication.processing.CalculatorTest
getSqrt
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.079 sec

------------- Standard Output ---------------

getSqrt

C:\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Users\Z\Documents\NetBeansProjects\DescriptiveStatisticsApplication\build\classes;C:\Program Files\NetBeans 8.2\platform\modules\ext\junit-4.12.jar;C:\Program Files\NetBeans 8.2\platform\modules\ext\hamcrest-core-1.3.jar;C:\Users\Z\Documents\NetBeansProjects\DescriptiveStatisticsApplication\build\test\classes;C:\Program Files\NetBeans 8.2\extide\ant\lib\ant-launcher.jar;C:\Program Files\NetBeans 8.2\extide\ant\lib\ant.jar;C:\Program Files\NetBeans 8.2\extide\ant\lib\ant-junit.jar;C:\Program Files\NetBeans 8.2\extide\ant\lib\ant-junit4.jar

------------- ---------------- ---------------

Update on 5/27:

5月27日更新:

After doing some tests from the suggestions in the comments, this unit test can now pass successfully. I think this question can be closed.

在根据评论中的建议进行一些测试后,此单元测试现在可以成功通过。我认为这个问题可以结束。

What I worry about is I didn't find out any reason why it failed and why now it succeed! Same code, same logic! The only thing I have done is like commenting out some non-important lines and then adding them back, changing some declaration statements sequence and then changing them back. The same code has different behaviors. I haven't changed anything in the building and running env and libs. This doesn't make any sense. The result from a piece of code should be reproducible.

我担心的是我没有找到任何失败的原因,为什么现在它成功了!相同的代码,相同的逻辑!我唯一做的就是注释掉一些非重要的行然后再添加它们,更改一些声明语句序列然后再更改它们。相同的代码有不同的行为。我没有在构建和运行env和libs中改变任何东西。这没有任何意义。一段代码的结果应该是可重现的。

1 个解决方案

#1


-1  

The float in Java is not precise and as you've already seen the result <4.000000000052429> In other words, you set a float number to 4 in java, it is not actually 4. You can use long or even BigInteger to solve this problem.

Java中的浮点数并不准确,因为您已经看到结果<4.000000000052429>换句话说,您在java中将浮点数设置为4,实际上不是4.您可以使用long或甚至BigInteger来解决此问题。

#1


-1  

The float in Java is not precise and as you've already seen the result <4.000000000052429> In other words, you set a float number to 4 in java, it is not actually 4. You can use long or even BigInteger to solve this problem.

Java中的浮点数并不准确,因为您已经看到结果<4.000000000052429>换句话说,您在java中将浮点数设置为4,实际上不是4.您可以使用long或甚至BigInteger来解决此问题。