On Sat, Oct 9, 2010 at 5:58 AM, Alexei Fedotov <[email protected]>wrote:
> I'm trying to understand best coding practices. Do you mean replacing
> assertTrue to assertEquals for assertions like a = b? isAvailable()
> assertions still may be implemented via assertTrue?
>
Mostly I prefer this:
assertEquals("hotdog", concatenate("hot", "dog"));
Over this:
assertTrue("hotdog".equals(concatenate("hot", "dog")));
If the expression fails, assertEquals() gives more detail in the failure
message. That's pretty non-controversial and we should prefer assertEquals()
when it applies.
It comes up less frequently, but I have some other preferences related to
better detail messages. When comparing arrays, equals() does the wrong
thing. To work around, I compare the string form for primitives:
byte[] actual = ...
byte[] expected = ...
assertEquals(Arrays.toString(actual), Arrays.toString(expected));
And the list form for reference types:
Object[] actual = ...
Object[] expected = ...
assertEquals(Arrays.asList(actual), Arrays.asList(expected));
I'm not actually planning on doing massive scrubbing to change styles or
anything. But when I investigate specific problematic tests, it's helpful to
fix the test to tell me everything that it can.
Cheers,
Jesse