JUnit assertEquals(double expected, double actual, double epsilon)

JavaJunit

Java Problem Overview


> Possible Duplicate:
> JUnit: assertEquals for double values

Apparently the assertEquals(double expected, double actual) has been deprecated.

The javadocs for JUnit are surprisingly lacking, considerings its wide use. Can you show me how to use the new assertEquals(double expected, double actual, double epsilon)?

Java Solutions


Solution 1 - Java

Epsilon is your "fuzz factor," since doubles may not be exactly equal. Epsilon lets you describe how close they have to be.

If you were expecting 3.14159 but would take anywhere from 3.14059 to 3.14259 (that is, within 0.001), then you should write something like

double myPi = 22.0d / 7.0d; //Don't use this in real life!
assertEquals(3.14159, myPi, 0.001);

(By the way, 22/7 comes out to 3.1428+, and would fail the assertion. This is a good thing.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuxuryModeView Question on Stackoverflow
Solution 1 - Javarajah9View Answer on Stackoverflow