Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock private method for testing using PowerMock?

I have a class which I would like to test with a public method that calls private one. I'd like to assume that private method works correctly. For example, I'd like something like doReturn....when.... I found that there is possible solution using PowerMock, but this solution doesn't work for me. How It can be done? Did anybody have this problem?

like image 639
Julie Langstrump Avatar asked Oct 18 '11 07:10

Julie Langstrump


People also ask

How do I use mock private PowerMock?

Method With No Arguments but With Return Value. As a simple example, let's mock the behavior of a private method with no arguments and force it to return the desired value: LuckyNumberGenerator mock = spy(new LuckyNumberGenerator()); when(mock, "getDefaultLuckyNumber"). thenReturn(300);

How do I make a private call in PowerMock?

Assume that this private method has to be unit tested for some reason. In order to do so, you have to use PowerMock's Whitebox. invokeMethod(). You give an instance of the object, method name as a String and arguments to call the method with.


1 Answers

I don't see a problem here. With the following code using the Mockito API, I managed to do just that :

public class CodeWithPrivateMethod {      public void meaningfulPublicApi() {         if (doTheGamble("Whatever", 1 << 3)) {             throw new RuntimeException("boom");         }     }      private boolean doTheGamble(String whatever, int binary) {         Random random = new Random(System.nanoTime());         boolean gamble = random.nextBoolean();         return gamble;     } } 

And here's the JUnit test :

import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.support.membermodification.MemberMatcher.method;  @RunWith(PowerMockRunner.class) @PrepareForTest(CodeWithPrivateMethod.class) public class CodeWithPrivateMethodTest {      @Test(expected = RuntimeException.class)     public void when_gambling_is_true_then_always_explode() throws Exception {         CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod());          when(spy, method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))                 .withArguments(anyString(), anyInt())                 .thenReturn(true);          spy.meaningfulPublicApi();     } } 
like image 63
Brice Avatar answered Sep 23 '22 22:09

Brice