[java] JUnit assertEquals(double expected, double actual, double epsilon)

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)?

This question is related to java junit

The answer is


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.)