TDD Anti-Pattern: The Nullifier
December 29th, 2006 by James CarrLately I’ve been encountering quite a bad smell in a few unit tests I’ve seen (and even a few I’ve written): tests that only contain assertNull and assertNotNull. Essentially, the tests will verify that a method call returns something besides null, but doesn’t verify the value of the object returned.
I initially noticed this when one of my co-workers (who has also read my TDD Anti-Patten list) called me over the other day to show me what he felt was the reason there was quite a few bugs in one of the classes I had written. As we looked through the unit test a partner and I had written, I noticed that we had started out small with each feature (which is a good thing!), checking that the methods worked and returned a value, but rather than following up and testing the actual behavior, we had instead opted to just move forward to the next feature, resulting in a unit test with 300 lines of null/not null assertions.
Avoidance is pretty simple… just follow up on small/simple test cases with test cases that really do test the behavior of the feature. In fact, if null/not null assertions don’t really provide any value and are really simply artifacts of small first steps, I believe it would be better to simply remove them and only keep the later tests that provide value by testing behavior, not language level semantics like nil values. However, if value can be gained from a null/not null assertion, then keep it! Just weigh the benefits.








