PiaklA
PiaklA

Reputation: 505

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.

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. Can anybody point me to an example ?

Upvotes: 0

Views: 835

Answers (2)

Parth Solanki
Parth Solanki

Reputation: 3448

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

Upvotes: 5

diegomtassis
diegomtassis

Reputation: 3657

You can't mock a static method invocation using EasyMock. 2 solutions:

  • 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). Partial mocks using easymock.
  • As someone mentioned above, use PowerMock and mock directly the static invocation.

Upvotes: 1

Related Questions