简体   繁体   English

使用简单的模拟对象

[英]Use Easy Mock Objects

I am working on a Junit Test where I need to work on Easymock and Class objects together to rub the test. 我正在进行Junit测试,我需要同时对Easymock和Class对象进行测试。

Following is my code snippet 以下是我的代码段

@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

}

I tried to see an example where I can use Easy mock and Class object together , I could not find that is suitable for my case. 我试图看到一个示例,其中我可以一起使用Easy模拟和Class对象,但找不到适合我的情况的示例。 Can anybody point me to an example ? 有人可以指出一个例子吗?

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. 
   }

You can't mock a static method invocation using EasyMock. 您不能使用EasyMock模拟静态方法调用。 2 solutions: 2个解决方案:

  • Extract the static invocation to a different method in your SUT and test a partially mocked version of your SUT (mocking only the new method where the static invocation is done). 将静态调用提取到SUT中的其他方法,并测试SUT的部分模拟版本(仅模拟完成静态调用的新方法)。 Partial mocks using easymock . 使用easymock进行部分模拟
  • As someone mentioned above, use PowerMock and mock directly the static invocation. 如上所述,使用PowerMock并直接模拟静态调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM