我正在进行Junit测试,我需要将Easymock和Class对象一起工作以进行测试。
以下是我的代码段
@Before
public void setUp() {
request=EasyMock.createMock(SlingHttpServletRequest.class);
response=EasyMock.createMock(SlingHttpServletResponse.class);
}
@Test
public void testImage() {
RequestContext ctx = new RequestContext();
// RequestContext and RequestContext Util are both classes defined in Project
expect(RequestContextUtil.setupContext(request,response)).andReturn(ctx);
// This line is throwing an error , so I am not able to add replay or verify method
}
我试图看到一个例子,我可以一起使用Easy mock和Class对象,我找不到适合我的情况。任何人都能指出我的一个例子吗?
答案 0 :(得分:5)
private MockHttpServletRequest request;
private MockHttpServletResponse response;
@Before
public void setup() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
@Test
public void testImage() {
//here you don't need to `expect` or `reply`
// `request` and `response` is mock now.
}
答案 1 :(得分:1)
您无法使用EasyMock模拟静态方法调用。 2解决方案: