在单元测试中使用EasyMock

我们每天的开发中都要编写大量的单元测试,很多单元测试的对象是相对独立的,例如一个所得税计算的模块,但是更多的单元测试需要依赖其他的组件或者服务。因此,我们需要EasyMock来无缝的连接这些组件。Mock的意思就是空的,不存在的东西。

EasyMock刚刚发布了2.2版本,它开始使用很多基于jdk 1.5的特性。

下面我们举一个例子:

假设定义一个如下的接口:

public interface ISimpsonService {
    IEpisode getEpisode(int number);
}

public interface IEpisode {
    int getNumber();
   String getTitle();
   InputStream getDataAsStream();
}

该接口的实现为

public class ClientSimpsonService implements ISimpsonService {
    private ISimpsonService remoteSimpsonService;
    public ClientSimpsonService(ISimpsonService remoteSimpsonService) {
        this.remoteSimpsonService = remoteSimpsonService;
    }
    public IEpisode getEpisode(int episodeNumber) {
        return null;
    }
}
对该服务的单元测试如下
public class ClientSimpsonServiceTest extends TestCase {
    public void testClientSimpsonService() {
        try {
            new ClientSimpsonService(null);
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected
        }
    }
我们可以看到因为没有remoteSimpsonService,所以我们只传一个null给ClientSimpsonService,
这样是无法完成测试的。所以我们把还没有写的remoteSimpsonService用mock替代。
import static org.easymock.EasyMock.createMock;
import junit.framework.TestCase;
public class ClientSimpsonServiceTest extends TestCase {
    private ISimpsonService remoteSimpsonServiceMock;
    protected void setUp() throws Exception {
        super.setUp();
        remoteSimpsonServiceMock = createMock(ISimpsonService.class);
    }
    public void testClientSimpsonService() {
        try {
            new ClientSimpsonService(null);
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected
        }
        new ClientSimpsonService(remoteSimpsonServiceMock);
    }
}
上面的黑体代码中,我们创建了一个ISimpsonService.class的mock,它拥有和ISimpsonService一样
接口。
同样的方法,我们可以定义ISimpsonService返回的对象IEpisode
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import junit.framework.TestCase;
public class ClientSimpsonServiceTest extends TestCase {
    private IEpisode episode17Mock;
    private ISimpsonService remoteSimpsonServiceMock;
    protected void setUp() throws Exception {
        super.setUp();
        episode17Mock = createMock(IEpisode.class);
        remoteSimpsonServiceMock =
            createMock(ISimpsonService.class);
    }
...
    public void testGetEpisode() throws Exception {
        expect(remoteSimpsonServiceMock.getEpisode(17))
            .andReturn(episode17Mock);
        replay(remoteSimpsonServiceMock);
        ISimpsonService clientSimpsonService = 
            new ClientSimpsonService(remoteSimpsonServiceMock);
        IEpisode result = clientSimpsonService.getEpisode(17);
        verify(remoteSimpsonServiceMock);
        assertEquals(episode17Mock, result);
    }
上文仅仅是抛砖引玉,想了解更多EasyMock,请参看
http://today.java.net/pub/a/today/2006/06/20/getting-started-with-easymock-2.html

你可能感兴趣的:(jdk,html,.net,JUnit,单元测试)