Java News from Thursday, July 15, 2004

Cenqua (nee Cortex) has released Clover 1.3.1_02, a $250 payware unit test coverage tool. Clover modifies the source code to enable it to follow which statements are executed when, and keeps a running count of how many times each statement is executed during the test suite. Any statement that executes zero times is not being tested. Unlike Jester, Clover only tests whether the tests execute each statement and follow each branch. (It occasionally misses branches on the edges of >= or <=.) It does not test whether the tests correctly detect bugs deliberately introduced into the code. On the other hand, it runs orders of magnitude faster than a tool like Jester does. It's easy easy to use Clover several times a day. Indeed you can use it after each change to the test suite. By contrast, a Jester run can take several days to complete. Ideally you'd want to use both a tool like Jester and a tool like Clover since they do different things.

New features in 1.3 include regular expression filtering that allows you to exclude certain methods and statements from coverage statistics, Ant tasks to clean and merge coverage databases, faster instrumentation, and Java 1.5 support. Rgeular expression filtering looks particularly useful. It's nice to have lots of pure green bars that indicate 100% code coverage. However, several of my classes have statements like this one:

try {
  // something I know will work, but the compiler doesn't; 
  // like writing data into a ByteArrayOutputStream
catch (Exception ex) { // that I am sure can't be thrown
  // just in case I'm wrong
  throw new RuntimeException("Something very weird is happening", ex)

I can't actually write unit tests that cover the catch block because if I'm right that block is unreachable. Now I can exclude blocks like those from the test coverage. At least in theory. I haven't actually figured out how to make this work yet. The documentation is one of Clover's few weak spots. It's not always easy to figure out where to look to understand a particular feature. :-(

Clover has been a major help in developing XOM. It has located numerous bugs in XOM over the last year, and is largely responsible for the completeness of XOM's test suite. It's also helped to optimize XOM for both speed and size by finding dead, unreachable code I could cut out. This is one of the few tools I can recommend unconditionally, and would certainly pay for if Cenqua hadn't given me a free copy. (That's not personal, by the way. They'll happily provide free copies to all open source projects.) Clover integrates with Ant, NetBeans, Eclipse, and IntelliJ IDEA. It can generate test coverage reports in XML, HTML, PDF, or via a Swing Viewer. Java 1.2 or later is required.