Posts Tagged ‘easymock’

EasyMock Capture Example

Thursday, September 2nd, 2010


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


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";

    public void setUp() throws Exception {
        controller = EasyMock.createStrictControl();
        dao = controller.createMock(Dao.class);
        service = new ServiceImpl();  
    public void testSave() {
        Capture <DomainObject> capturedArgument = new Capture <DomainObject>();, EasyMock.isA(DomainObject.class)));
        DomainObject record = capturedArgument.getValue();
        assertEquals(description, record.getDescription());
        assertEquals("getNextId", record.getId());

Source code for classes under test after the break…….

Tags: , ,
Posted in Development, Examples | No Comments »