Posts Tagged ‘easymock’
EasyMock Capture Example
Thursday, September 2nd, 2010
Background
EasyMock version 2.4 introduced some new functionality – the ability to capture the arguments passed to mock objects. As ever a few lines of code speaks volumes.
Capture Example
Test Class
package foo.bar; import static org.junit.Assert.assertEquals; import org.easymock.Capture; import org.easymock.EasyMock; import org.easymock.IMocksControl; import org.junit.Before; import org.junit.Test; public class TestServiceImpl { private Service service; private Dao dao; private IMocksControl controller; private final String description = "description"; @Before public void setUp() throws Exception { controller = EasyMock.createStrictControl(); dao = controller.createMock(Dao.class); service = new ServiceImpl(); service.setDao(dao); } @Test public void testSave() { Capture <DomainObject> capturedArgument = new Capture <DomainObject>(); dao.save(EasyMock.and(EasyMock.capture(capturedArgument), EasyMock.isA(DomainObject.class))); controller.replay(); service.save(description); controller.verify(); DomainObject record = capturedArgument.getValue(); assertEquals(description, record.getDescription()); assertEquals("getNextId", record.getId()); } }
Source code for classes under test after the break…….
(more…)
Tags: easymock, java, testing
Posted in Development, Examples | No Comments »