... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2008-07-31

JUnit Gotcha

In JUnit 4, if you use the version of assertEquals that compares two objects, and those objects happen to be subclasses of Number, the framework converts them to long before comparing them.  This is broken for any non-integral type since it will neglect the fractional part.

Just to make matters worse, if you attempt to compare two floating point numbers using assertEquals and you forget to add the 'delta' argument, then once again you will get the object-version of the method and the comparison will be invalid.  This is a regression from the old JUnit which predated auto-boxing and the compiler would moan about mismatched argument types.  It is hard to believe, but this unit test passes:

@Test
public void test() {
    assertEquals(1.2, 1.3);
}

Blog Archive