There is NO assert method in JUnit with the signature
assertEquals(Double expected, Double result);
There is one, however, generic for objects:
assertEquals(Object expected, Object result);
This calls the objects' equals
method and as you can expect, it is not recommended to use this for comparing Double
objects.
For doubles, as you observed, it is absolutely necessary to use a delta for comparison, to avoid issues with floating-point rounding (explained already in some other answers). If you use the 3-argument version of assertEquals
with double
arguments
assertEquals(double expected, double actual, double delta);
your Double
s will get silently unboxed to double
and everything will work fine (and your tests won't fail unexpectedly :-).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…