JMock vs EasyMock Smackdown
Thursday, January 22, 2009 at 12:55AM Lets see which of the two popular Java test object mocking tools is more straightforward. Cast your vote! (in comments)
| with jMock | with EasyMock |
| you need to import | |
| import org.jmock.Expectations; | import static org.easymock.classextension. EasyMock.createMock; import static org.easymock.classextension. EasyMock.expect; import static org.easymock.classextension. EasyMock.expectLastCall; import static org.easymock.classextension. EasyMock.replay; import static org.easymock.classextension. EasyMock.verify; |
| your test class needs to extend | |
| MockObjectTestCase | TestCase |
| in setUp() method | |
| mockedThing = mock(SomeInterface.class) | mockedThing = createMock(SomeInterface.class) |
| to set expectations | |
| checking( new Expectations() {{ // this run does not throw one(mockedThing).run(); // expect an exception // expect multiple runs | // since run() returns void expect(mockedThing.run()); // or if the method returns non-void // expect an exception andThrow(new RuntimeException()); // expect multiple runs |
| to put into replay mode | |
| N/A | replay(mockedThing); // only after this call the mock is usable |
| to use the mock in tests | |
| RealThing realThing = new RealThing(mockedThing); // this will call the mocked thing at some point | RealThing realThing = new RealThing(mockedThing); // this will call the mocked thing at some point |
| to verify mock | |
| N/A | verify(mockedThing); // if you forget this, you will not know if the // expectations were fulfilled |
| thread safe | |
| ??? | mockedThing is not thread-safe.... use this to make it so: makeThreadSafe(mockedThing, true); |
