Junit Mocking Private Method

Mohan

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;
    }    
}
RicardoS

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

mocking private variables in a function for testing Junit4

Mocking private method call using PowerMockito returns null instead of returning List<String>: (Want not to execute private method)

Mocking private method of class under test using JMockit

Mocking public method inside a private method

JUnit5 assertAll private method assertions being ignored

Mocking a Private Object

How to test method in Junit4 which calls a private method

Junit Test Case for private method

Mocking objects for JUnit test

Mocking ReentrantReadWriteLock in JUnit with mockito

Mocking private method with PowerMock

Forcing private method return value in test JUnit

Mocking a private constructor

Why mocking a private method enters method?

Junit Mockito not mocking as expected

Error in junit while mocking

Asserting Exceptions for private method in JUnit

Junit Mocking/Stubbing method B inside method A (Non Parameterized method A)

PowerMockito: mocking private method and get a value without accessing it

How to match 'any' parameter type while mocking private method in Jmockit

Why when mocking a private method using Powermock is calling the real method?

Mocking a private method in junit using Powermockito

Why mocking private field is bad?

Junit ignore method mocking

Powermock throws InvalidUseOfMatchersException when mocking private method

Junit5: WebMvcTest returns 404. Probably because I'm not mocking underlyng method?

Mocking private method with PowerMockito only works starting with second method call

Mocking an object returned from private method in java

LoadingCache mocking for JUnit testing