您的位置:

EasyMock:一种Mock框架

一、EasyMock概述

为了测试一个系统或组件,往往需要对其依赖的组件进行模拟或者mock。这种模拟的方法叫做mocking,非常多的mock框架纷纷出现。EasyMock是一种流行的Java mocking框架,它通过生成固定的mock对象来帮助程序员测试代码在不同环境下的运行情况。EasyMock能够简化一些测试工作,提高测试代码的可读性和可维护性

二、EasyMock基本用法

在程序中集成EasyMock,需要先导入EasyMock相关的jar包。mock一个类的示例如下:

public class TestEasyMock {
    public static void test() {
        // 利用createMock()生成一个mock对象
        List mockList = EasyMock.createMock(List.class);
        // 为mock对象设置预期的行为,此处的调用可以不用真实连接到List。后期程序调用此mock对象的时候,将会得到预期的返回值。
        EasyMock.expect(mockList.get(0)).andReturn("hello");
        // 激活mock对象
        EasyMock.replay(mockList);
        // 断言以及期望的结果
        Assert.assertEquals("hello", mockList.get(0));
        // 对使用过的mock对象进行校验和清空,防止下一次使用不正常
        EasyMock.verify(mockList);
    }
}

三、从多个方面玩转EasyMock

1、EasyMock更改url

在测试开发中经常需要使用mock技术对后台的数据流进行模拟获取,进行业务的测试。这时有时候需要修改接口的访问url才能够进行数据获取,可以利用EasyMock对url进行更改。

public class TestEasyMock {
    public static void testEasyMockUrlChange() {
        String url = "https://www.baidu.com";
        String expectedReturnValue = "mock html value";
        HttpMethod method = HttpMethod.GET;
        EasyMock.expect(mockServer.expect(HttpRequest.request().withMethod(method).withPath(url))).andRespond(HttpResponse.response().withStatusCode(HttpStatus.OK).withBody(expectedReturnValue));
        EasyMock.replay(mockServer);
    }
}

2、EasyMock在线环境模拟post应答

对于一些需要模拟post的应答,利用EasyMock进行模拟的时候需要考虑一些特殊情况,比如接口需要在一个登录成功的环境中才允许post。可以在EasyMock在线环境中进行模拟。

public class TestEasyMock {
    public static void testEasyMockOnline() {
        PostMethod postMethod = new PostMethod("https://www.baidu.com");
        String expectedContent = "hello world";
        EasyMock.expect(mockServer.post(HttpRequest.request().withPath("/post")).andRespond(HttpResponse.response().withStatusCode(HttpStatus.OK).withBody(expectedContent)));
        EasyMock.replay(mockServer);
    }
}

3、EasyMock模拟post

在一些测试开发中需要利用mock对象进行接口的访问以及数据交互,EasyMock可以轻松实现这样的需求。

public class TestEasyMock {
    public static void testEasyMockPost() {
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setCookiePolicy("rfc2109");
        httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0");
        httpClient.getState().addCookies(mockCookies);
        String loginUrl = "http://localhost:8080/ajaxlogin.jsp";
        PostMethod postMethod = new PostMethod(loginUrl);
        EasyMock.expect(mockHttpClient.executeMethod(postMethod)).andReturn(HttpStatus.SC_OK);
        EasyMock.replay(mockHttpClient);
    }
}

4、EasyMock模拟post应答

在EasyMock中需要模拟接口返回的数据,在post应答方面需要设置响应的状态码以及返回内容

public class TestEasyMock {
    public static void testEasyMockPostReply() {
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setCookiePolicy("rfc2109");
        httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0");
        httpClient.getState().addCookies(mockCookies);
        String url = "http://localhost:8080/ajaxpost.jsp";
        PostMethod postMethod = new PostMethod(url);
        String expectedContent = "mock html value";
        EasyMock.expect(mockServer.expect(HttpRequest.request().withMethod(HttpMethod.POST).withPath(url))).andRespond(HttpResponse.response().withStatusCode(HttpStatus.OK).withBody(expectedContent)));
        EasyMock.replay(mockServer);
    }
}