Ugo Cei wrote:
Il giorno 22/ott/04, alle 19:40, Reinhard Poetz ha scritto:
Have you considered using Easymock? IMHO its usage is more intuitive
than jMock.
(see BlockDeployer test cases)
After perusing the documentation and samples, I decided that I liked
jMock more, but I didn't try EasyMock in practice. If you are familiar
with it, you might try doing a version of the
ResourceExistsActionTestCase that I just refactored, so we can do a
comparison and decide which is better.
Surce, here we go:
public void testAct() throws Exception {
// create objects and controls
String src = "don't care";
ResourceExistsAction action = new ResourceExistsAction();
MockControl resolverCtrl = MockControl.createControl(SourceResolver.class);
SourceResolver resolver = (SourceResolver) resolverCtrl.getMock();
MockControl sourceCtrl = MockControl.createControl(Source.class);
Source source = (Source) sourceCtrl.getMock();
MockControl parameterCtrl =
MockClassControl.createControl(Parameters.class);
Parameters parameters = (Parameters) parameterCtrl.getMock();
// "record" excpected method calls, return values and exceptions
source.exists();
sourceCtrl.setReturnValue(true, MockControl.ONE);
sourceCtrl.replay();
resolver.resolveURI(src);
resolverCtrl.setReturnValue(source, MockControl.ONE);
resolver.release(source);
resolverCtrl.replay();
parameters.getParameter("url", src);
parameterCtrl.setReturnValue(src);
parameterCtrl.replay();
// call method to be tested
Map result = action.act(null, resolver, new HashMap(), src, parameters);
// check result
assertSame("Test if resource exists", AbstractAction.EMPTY_MAP, result);
// check mock objects if methods have been called correctly
resolverCtrl.verify();
parameterCtrl.verify();
sourceCtrl.verify();
}
However, I suspect it's more a
matter of taste than anything else.
Probably yes. IMO the advantages of Easymock are
- you directly call methods on the objects -> so refactoring is rather simple
because the TestMethods are updated too
- Easymock can also mock classes and not only interfaces
Ugo, how did you mock the Parameters object?
IMO the disadvantage is that it is more verbose than jMock.
Of course, if you rewrite our tests, it's your decision :-)
--
Reinhard