This post originated from an RSS feed registered with Java Buzz
by Charles Miller.
Original Post: Nothing is too trivial to test
Feed Title: The Fishbowl
Feed URL: https://fishbowl.pastiche.org/atom.xml
Feed Description: tail -f /dev/mind > blog
It was really trivial code. A wrapper around a HashMap (dictionary) to temporarily cache some values that were getting too expensive to calculate every time. While writing it, I pretty much convinced myself that this code was so simple there was really no point writing unit tests, but as I got close to checking the code in I realised that the first thing my code-reviewer would ask was âwhereâs the test?â
So I wrote a test. And it failed. It failed because I'd made a really dumb typo in the constructor of one of the nested classes I was using as cache keys, writing this.username = (user == null) ? null : username instead of this.username = (user == null) ? null : user.getName(). This simple mistake meant my cache would be at best useless, and far more often entirely inaccurate.
Lessons for the day, there are two:
Nothing is too trivial to test
Even the mere threat of a code-review leads to better code