Thursday, September 3, 2009

Unit testing tools, a minimalist toolset.

After wading through the sea of unit testing tools out there, here is a minimalist (and so far, sufficient) set of open-source tools for testing server-side centric Java code.


JUnit 4.x

That is almost a given, full integration with Eclipse and Ant, standard in the industry, etc, etc. JUnit 4.1 is the last release where I did some thorough investigation of any new function, though the just released JUnit 4.7 (Aug/4th) has some function I would like to explore.


DDTUnit (

This is the best alternative for data-driven testing, where the test code is small but has to use different data inputs.

Side comment:This presentation cross-compares DDTUnit with other data-driven approaches:

Tip: The syntax for the test XML data can be a chore, but this Eclipse snippet (with instructions) can help:


JMock (

This library allows you to pre-program objects in the JUnit testcase with the "right" responses to exercise the target code.


JMockit (

This library is used to replace Java calls ordinarily not under your control with code under your control.

For instance, you can intercept a call to DriverManager.getConnection and return your own implementation of java.sql.Connection.

Tip 1: This library requires you to add -java.agent=build_dir/libraries/test/jmockit/jmockit.jar to the unit execution path.
Tip 2: Avoid it if you can, it makes tests slower and harder to follow. If you have another entry point to pass a JMock object, such as setter method, do it.

Links I have bookmarked about unit testing recently:


  1. About JMockit:
    1) It does not require the use of "-javaagent". The recommended way is to simply add tools.jar (from a JDK 1.6 installation) to the classpath (besides jmockit.jar), while having all test classes use one of the JMockit-JUnit/TestNG integration mechanisms (@RunWith(JMockit.class), ...).

    2) All mocking tools (jMock, EasyMock, Mockito, JMockit, etc.) add some runtime overhead, but from my experience so far JMockit adds the least amount of overhead.

    3) The behavior-based mocking API provided by JMockit is probably the cleanest of all, and certainly the most consistent one. The JMockit full distribution contains sample test suites for comparison with all other major tools.

  2. Thanks for the corrections. I had forgotten about Tip 2. This is the repurposing of an old blog entry posted inside the firewall.

    I have since revised those views after seeing the latest updates to JMockit and have completely dropped JMock from my list.

    I posted a new slideshare to my team where I call out JMockit as the tool for the job.

  3. Hi Denilson,
    great presentation about DDTUnit - just stumbled over this.