Note that if you're not doing math, there's nothing wrong with asserting exact floating point values. For instance:
public interface Foo {
double getDefaultValue();
}
public class FooImpl implements Foo {
public double getDefaultValue() { return Double.MIN_VALUE; }
}
In this case, you want to make sure it's really MIN_VALUE
, not zero or -MIN_VALUE
or MIN_NORMAL
or some other very small value. You can say
double defaultValue = new FooImpl().getDefaultValue();
assertEquals(Double.MIN_VALUE, defaultValue);
but this will get you a deprecation warning. To avoid that, you can call assertEquals(Object, Object)
instead:
// really you just need one cast because of autoboxing, but let's be clear
assertEquals((Object)Double.MIN_VALUE, (Object)defaultValue);
And, if you really want to look clever:
assertEquals(
Double.doubleToLongBits(Double.MIN_VALUE),
Double.doubleToLongBits(defaultValue)
);
Or you can just use Hamcrest fluent-style assertions:
// equivalent to assertEquals((Object)Double.MIN_VALUE, (Object)defaultValue);
assertThat(defaultValue, is(Double.MIN_VALUE));
If the value you're checking does come from doing some math, though, use the epsilon.