Cleaner JUnit tests with PMD

14 Apr 2006

Do you have any JUnit tests that have assertions like this?

List list = getList();
assertFalse(!list.isEmpty());

Whew, that's a nasty assertFalse. Let's see, List.isEmpty(), but the negation of that, and we don't want that negation to be true... yikes. Far better to rewrite this as:

List list = getList();
assertTrue(list.isEmpty());

Much, much clearer. Not coincidentally, the recently released PMD 3.6 has a SimplifyBooleanAssertion rule that will catch just this problem. Thanks to Xavier Le Vourch for coding up this rule! And thanks to Elliotte Rusty Harold for mentioning it on Cafe Au Lait.