Saturday, October 03, 2009

Joys of unit testing: finding a 5 year old bug (dormant, harmless, but a bug nonetheless)

Well here is something I don't do every day: write a unit test mostly to improve code coverage metrics, and find a dormant, almost 5-year old bug. But that's exactly what I did today: the bug was found in Jackson i/o package. And, it turns out, in 2 other libraries (Woodstox, Aalto) as well: code has been evolved over time, being originally written for Woodstox (checked in almost 5 years ago), and finding its way into Aalto and Jackson later on.

So what was the bug? It was a bug in 'InputStream.skip()' method, which skipped different number of bytes than it reported; return value was number that was requested, not number that was actually skipped. Simple case of mistaken variables.

And why was this bug not found earlier? Because this code path was never really executed -- just because skip() method exists in InputStream doesn't mean it is used. Except when someone writes a unit test to exercise it.

In the end I guess there are 2 complementary ways to think about this: first, try to unit test all code you have; and second, do not write code you do not plan to use. That is, given that I wasn't expecting to ever use this method, perhaps I should have written it to throw an IOException right away. And if was ever needed, then (and only then!) write it and add unit tests.

blog comments powered by Disqus

Sponsored By


Related Blogs

(by Author (topics))

Powered By

About me

  • I am known as Cowtowncoder
  • Contact me at@yahoo.com
Check my profile to learn more.