java开源项目:http://easymock.org/
这里简单记录一下使用方法:
1.下载easymock.jar,放到项目lib目录下。
2.测试代码:
HttpServletRequest request = EasyMock
.createMock(HttpServletRequest.class);
EasyMock.expect(request.getParameter("ffff")).andReturn("33333");
EasyMock.expect(request.getParameter("ffff")).andReturn("444");
EasyMock.expect(request.getRealPath("/")).andReturn("fffffffff")
.anyTimes();
EasyMock.replay(request);
System.out.println(request.getRealPath("/"));
System.out.println(request.getParameter("ffff"));
System.out.println(request.getRealPath("/"));
System.out.println(request.getParameter("ffff"));
EasyMock.verify(request);
3.creatMock 生成一个mock对象。
.createMock(HttpServletRequest.class);
EasyMock.expect(request.getParameter("ffff")).andReturn("33333");
EasyMock.expect(request.getParameter("ffff")).andReturn("444");
EasyMock.expect(request.getRealPath("/")).andReturn("fffffffff")
.anyTimes();
EasyMock.replay(request);
System.out.println(request.getRealPath("/"));
System.out.println(request.getParameter("ffff"));
System.out.println(request.getRealPath("/"));
System.out.println(request.getParameter("ffff"));
EasyMock.verify(request);
4.expect 注入方法调用后的返回值,可以设置调用次数,每次的返回值可以不同。
5.replay 切换到播放模式,调用次数超过设置值时会报错。
6.verify 回收。