JUnit and auto-boxing
After that gigantic previous post, I thought it'd be good to post something simpler and more widely applicable.
I came across a little JUnit assertion gotcha that had me puzzled for a second. I was testing this (note, calculateDurationInSeconds() returns a long)
I came across a little JUnit assertion gotcha that had me puzzled for a second. I was testing this (note, calculateDurationInSeconds() returns a long)
assertEquals(120, subjectUnderTest.calculateDurationInSeconds());and getting the following, less-than-helpful test failure message
expected: <120> but was: <120>I figured this one out pretty quickly because my Eclipse underlines cases of auto-boxing/unboxing. Since the assertEquals(Object, Object) method is being used, my int is becoming an Integer and the long returned by the method is becoming a Long. Obviously, any equals() comparaison is going to fail. I corrected this by using the long literal notation:
assertEquals(120L, subjectUnderTest.calculateDurationInSeconds());What with all the conversion already going on between the presentation and data layers, I'm trying to generalise the practice of making primitive/wrapper conversions explicit, just in case.
