EasyMock for JUnit tests

January 8, 2007

In the Virtual Workspace Service TP1.2.2 (just released) we used EasyMock for unit tests:

http://www.easymock.org

“EasyMock provides Mock Objects for interfaces in JUnit tests by generating them on the fly using Java’s proxy mechanism. Due to EasyMock’s unique style of recording expectations, most refactorings will not affect the Mock Objects.”

I am pretty happy with this package. For testing our internal state transition logic I could pre-record the exact methods on the task object that were expected to be called (including argument matching) for a certain transition, test that the expectations were met, and simultaneously test that the wrong tasks were not called.

We have around 30 internal states, around 15 types of tasks, and the tasks are launched out of band via work queue threads, so it was helpful to abstract out as much as possible and just concentrate on testing the transitions and the resulting tasks that were launched.

Having internal subsystem plugins that are instantiated via JNDI helped. Using a test JNDI config to override a factory implementation with a new one that returns mock objects, it was straightforward to isolate the code under test.

This will be good for testing a lot of things: it’s designed expressly to let you mock up the “collaborators” of a subsystem and make sure the interactions are correct without running code in the subsystems that are not being tested.