0

I need to mock private method and should return true. In ServiceImpl-execute() my request will go to else { } and it will call "eventRequest()". Its a private boolean eventRequest(), So whenever evenRequest() will call i should return true. Can anybody help me out

ServiceImplTest.java

@RunWith(PowerMockRunner.class)
@PrepareForTest({ServiceImpl.class})
public class ServiceImplTest {

    @Test
    public void testExecute() {
        Response response = serviceImpl.execute(request);
        Assert.assertNotNull(pushResponse);
        Assert.assertEquals(true, pushResponse.isIsSuccess());
    }
}

ServiceImpl.java

public class ServiceImpl {
    public Response execute(Request request) {
        Response response = null;
                boolean isSuccess;
                if (returnMockResponse(request, notifyRqst)) {
                    isSuccess = true;
                } else {
                    isSuccess = eventRequest(notifyXmlRqst);
                }
        response = ResponseBuilder.createResponse(isSuccess);
        return response;
    }

    // Need to mock below private method and should return true.
    private boolean eventRequest(String request) throws Exception {
        return eventNotifyResponse.isIsSuccess();
    }
}

ResponseBuilder.java

public class ResponseBuilder {
    public Response createResponse(boolean result) {
            Response response = new Response();
            response.setIsSuccess(result);
            return response;
    }    
}
Mohan
  • 59
  • 1
  • 7
  • 2
    Don't mock a private method. Just mock the eventNotifyResponse to return true when calling isIsSuccess(). Also, why are you passing an unused String to the method? – Manu Jul 09 '15 at 13:38
  • Have some condition check with that string so i'm passing string. – Mohan Jul 09 '15 at 13:47
  • @Manu , We can't do that because before calling eventNotifyResponse.IsSuccess(); we have some more codes to verify some other logic but i want to skip that and return true, when we call eventRequest(notifyXmlRqst) – Mohan Jul 09 '15 at 13:55
  • Have a look to this [other question](https://stackoverflow.com/questions/7803944/how-to-mock-private-method-for-testing-using-powermock). Posible duplicate – troig Jul 09 '15 at 14:02

3 Answers3

2

You can create a mock of eventNotifyResponse normally, then use Whitebox to set the private (internal) field.

Assuming your field eventNotifyResponse was of a type named EventNotifyResponse, the test class it would be something like:

    EventNotifyResponse evtNotifyResponseMock = mock(EventNotifyResponse.class);
    when(evtNotifyResponseMock.isIsSuccess()).thenReturn(true);
    Whitebox.setInternalState(serviceImpl, "eventNotifyResponse", evtNotifyResponseMock);

Whitebox is a class of Powermock (org.powermock.reflect.Whitebox).
setInternalState is overloaded. In the example, the parameters used are:

  1. the target object to inject into (your object being tested)
  2. the name of the internal field to be setted (String)
  3. the value itself, in this case a mock
RicardoS
  • 2,088
  • 1
  • 21
  • 22
0

Its not possible in jUnit or jMock by default, you can either change the private to public or you can invoke the public method which internally invokes the private method, and make sure that input causes the private method to get called and covered.

0

Have you try with PowerMock.createPartialMock(ClassWithPrivateMethod.class, "nameOfTheMethodToMock") and PowerMock.expectPrivate(mockObject, "nameOfTheMethodToMock", argument1, argument2) as describe in the wiki off powermock ?

https://code.google.com/p/powermock/wiki/MockPrivate

or perhaps see the answer of this question ...

How to mock private method for testing using PowerMock?

Community
  • 1
  • 1
Dams
  • 997
  • 12
  • 28