How to define a private testing method using PowerMock?

I have a class that I would like to test using a public method that calls a private one. I would suggest that the private method works correctly. For example, I would like something like doReturn....when... I found that there is a possible solution using PowerMock , but this solution does not work for me. How can I do that? Has anyone had this problem?

+59
java junit testing mockito powermock
Oct 18 2018-11-11T00:
source share
5 answers

I do not see a problem here. With the following code using the Mockito API, I was able 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 is 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(); } } 
+92
Oct 18 2018-11-11T00:
source share

A common solution that will work with any testing environment (if your class is not final ) is to manually create your own layout.

  • Change your private method to secure.
  • In the test class, extend the class
  • override a previously closed method to return any constant you want

This does not use the framework, so it is not so elegant, but it will always work: even without PowerMock. Alternatively, you can use Mockito to complete steps 2 and 3 for you if you have already taken step 1.

To make fun of a private method directly, you will need to use PowerMock, as shown in another answer .

+21
Oct 19 '11 at 19:03
source share

I know the ny method, which you can call a private function for testing in mockito

 @Test public void commandEndHandlerTest() throws Exception { Method retryClientDetail_privateMethod =yourclass.class.getDeclaredMethod("Your_function_name",null); retryClientDetail_privateMethod.setAccessible(true); retryClientDetail_privateMethod.invoke(yourclass.class, null); } 
+1
Aug 24 '17 at 8:21 on
source share

For some reason, Bryce's answer doesn't work for me. I was able to manipulate it a bit to make it work. It may just be because I have a new version of PowerMock. I am using 1.6.5.

 import java.util.Random; 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; } } 

The testing class is as follows:

 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.doReturn; @RunWith(PowerMockRunner.class) @PrepareForTest(CodeWithPrivateMethod.class) public class CodeWithPrivateMethodTest { private CodeWithPrivateMethod classToTest; @Test(expected = RuntimeException.class) public void when_gambling_is_true_then_always_explode() throws Exception { classToTest = PowerMockito.spy(classToTest); doReturn(true).when(classToTest, "doTheGamble", anyString(), anyInt()); classToTest.meaningfulPublicApi(); } } 
+1
Apr 6 '18 at 16:26
source share

Without arguments:

 ourObject = PowerMockito.spy(new OurClass()); when(ourObject , "ourPrivateMethodName").thenReturn("mocked result"); 

With String argument:

 ourObject = PowerMockito.spy(new OurClass()); when(ourObject, method(OurClass.class, "ourPrivateMethodName", String.class)) .withArguments(anyString()).thenReturn("mocked result"); 
-one
Sep 18 '19 at 11:24
source share



All Articles