Eric Dobbs introduced me to easymock a couple of weeks ago. This java library lets you incorporate mock object testing into junits. I’ve been looking for a good place to try it out and settled on a JMS-based distributed caching system that I’m writing for my current employer.
The old way to junit the onMessage method of a MessageListener requires a running JMS server, an InitialContext, a connection, a session, and a call to session.createObjectMessage to create an ObjectMessage. You then set the payload on the ObjectMessage and invoke onMessage, passing in the ObjectMessage as the parameter.
But with easymock, you simply create a mockObjectMessage, specify the payload object to be returned when message.getObject is called, and then invoke onMessage, passing in the mockObjectMessage as the parameter. No JMS server, no initial context, no session, and no connection are required. The result of this approach is a unit test that requires much less infrastructure.
4:06:19 PM
|
|