A Decorator for Leaky Tests

by Charles Miller on December 4, 2003

One of the perils of maintaining a suite of unit tests is the need for each test to be run against some stable baseline. Any resource that is changed by a test must be cleaned up afterwards, so that when you run all your tests as a single suite, they don't interfere with each other.

You can go a long way to avoiding this by using component models that allow you to replace components with mock objects and with in-memory databases that can be completely disposed of between tests, but there's always something that can leak, and if it can leak, Murphy's law kicks in with a vengeance.

A test that doesn't clean up after itself is a menace because if it causes an error, the error will only be apparent in the next test that relies on that particular resource... which means a long, frustrating search backwards through the test suite to find the last person to play with that particular piece of code.

Enter the LeakDetectorTest.

In the JUnit extensions package lies the TestDecorator. Like the name suggests, a TestDecorator wraps around a test, allowing you to extend it without changing the original class. Decorators allow you to add time constraints to a test, or repeat a test a certain number of times, or in the case of the LeakDetectorTest, add a post-condition check to each test class to see if it was the one that had forgotten to tear down properly.

The only problem, of course, is that the decorator has to be applied to every single test class. Which means one gigantic suite() method that would be tedious to write, and even more tedious to maintain. In a more dynamic language I could have temporarily added the check to the TestCase superclass, or applied an aspect to every test* method instead, but neither were possible in this case.

This is why every programmer should know at least one scripting language.

It took about a minute to write a Ruby five-liner that would generate my test suite for me, populating it with every test class in the project, each decorated with the LeakDetectorDecorator. Once you've got the script, it's a simple matter, whenever you find something isn't being cleaned up when it should, of building a new decorated suite to track down the leak, and fixing it.

Previously: Happy Birthday to Me

Next: Sick