Why are my assertEquals not working?
I am testing one of my methods double getSqrt(double s)
and having the following JUnit test code:
@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 as follows:
AssertionFailedError: expected: <4.0>; but was: <4.000000000052429>
I am using JUnit 4.12 and Hamcrest 1.3.
And my import:
import org.junit.Test;
import static org.junit.Assert.*;
Did I misuse the assumption?
Console output:
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 5/27:
After running some of the tests from the comments in the comments, this unit test can now pass successfully. I think this question can be closed.
I'm worried that I haven't found a reason why this failed, and why it works now! Same code, same logic! The only thing I did was comment out some of the non-important lines and then add them back, change some of the declaration statements and then change them. The same code has different types of behavior. I didn't change anything in the building and ran env and libs. It doesn't make any sense. The result from the code snippet should be reproducible.
source to share