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

Madhujith

I'm writing java unit test for a legacy code and I'm new to this area as well. I have to test following scenario (write unit test case for the testableMethod()). So, without executing code inside the getMode() method, I want to get a value for the mode variable.

Class A{

 public boolean testableMethod()
 {
   //code
   ......
   int mode = getMode();
   ......
   //do something with mode
   return X;
 }

 private int getMode()
 {
   return ComplexCalls(ComplexMethodCalls(), more());
 }

}

I've tried to do it using PowerMockito without getting success. It is possible to mock these kind of scenario with PowerMockito?.

gontard

You can with a PowerMockito spy:

public class A {
    public boolean testableMethod() {
        return getMode() == 1;
    }

    private int getMode() {
        return 5;
    }
}

import static org.junit.Assert.assertTrue;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.spy;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class ATest {
    @Test
    public void testableMethod_should_do_this() throws Exception {
        A a = spy(new A());

        doReturn(1).when(a, "getMode");

        assertTrue(a.testableMethod());
    }
}

See all this full example of partial mocking of a private method

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

PowerMockito verify private method called x times

How to get value from a private method's return value in test

How can I mock private static method with PowerMockito?

PowerMockito mocking static method fails when calling method on parameter

Mocking public method inside a private method

PowerMockito: got InvalidUseOfMatchersException when use matchers mocking static method

Powermockito private method mock NullPointerException. Calls the private method

Mocking private method with PowerMock

Mockito / Powermockito mock private void method

Why mocking a private method enters method?

How to Mock private method which is called from the constructor using PowerMockito

PowerMockito is calling real method instead of mocked private one

Mocking a static method using powermockito

powermockito spy private void method invocation

Mock a private void method using PowerMockito

Mocking static class with powermockito

How to get value without accessing database?

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

Junit Mocking Private Method

Mocking a private method in junit using Powermockito

Mocking method for all instances of class with PowerMockito

Verify private static method on final class gets called using PowerMockito

React Axios Get method : Accessing key, value elements from the response

Powermock throws InvalidUseOfMatchersException when mocking private method

Get Private Method Value at Public Method in JavaScript

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

Mocking an object returned from private method in java

Mock a method in the class under test without PowerMockito