JUnitassertEquals (double  期望值、 double 實際、 double epsilon)

可能的复制品:
JUnit: assertEquals for double value

显然 assertEquals(double expected, double actual)已经被废弃了。

考虑到它的广泛使用,JUnit 的 javadocs 出人意料地缺乏。你能告诉我如何使用新的 assertEquals(double expected, double actual, double epsilon)吗?

179157 次浏览

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