Junit difference between assertEquals(Double, Double) and assertEquals(double, double, delta)

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 Doubles will get silently unboxed to double and everything will work fine (and your tests won't fail unexpectedly :-).


Double math rarely if ever gives exactly equal results. For example, 0.1 * 0.1 != 0.01. You usually need at least some delta in comparing double-precision results.

On the other hand, if you're comparing boxed Doubles, it assumes you want the exact equality. Java doesn't have a default delta value taken into account, but Double.equals has slightly different behavior from ==: in particular, its handling of NaNs.

This makes sense in testing, because Double.NaN != Double.NaN, but in a test, if you expected an NaN and NaN was returned, that's a correct answer.


Better write something like this:

assertEquals(23.0, 250.0, 0.0)  

0.0 - it is delta. Read why yours methods are deprecated.