Note that there are some explanatory texts on larger screens.

plurals
  1. POMocked private method with PowerMock, but underlying method still gets called
    text
    copied!<p>I am trying to mocking to mock out a private method that is making a JNDI call. When that method gets called from a unit test, it throws an exception^. I would like to mock-out that method for testing purposes. I used the <a href="https://stackoverflow.com/questions/7803944/how-to-mock-private-method-for-testing-using-powermock/7804428#comment9868717_7804428">sample code from another questions answer</a>, and while the test passes, it seems that the underlying method still gets called. I inserted a <code>System.err.println()</code> in the <code>doTheGamble()</code> method, and it gets printed out to my console.</p> <p>Interesting enough, if I comment out the first <code>assertThat</code>, the test passes. ?:(</p> <p>So, how do I mock out a private method so that it does not get called?</p> <pre><code>import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; 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; import java.util.Random; 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; @RunWith(PowerMockRunner.class) @PrepareForTest(CodeWithPrivateMethod.class) public class PowerMock_Test { static boolean gambleCalled = false; @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); /* 1 */ assertThat( PowerMock_Test.gambleCalled, is(false) ); spy.meaningfulPublicApi(); /* 2 */ assertThat( PowerMock_Test.gambleCalled, is(false) ); } } class CodeWithPrivateMethod { public void meaningfulPublicApi() { if (doTheGamble("Whatever", 1 &lt;&lt; 3)) { throw new RuntimeException("boom"); } } private boolean doTheGamble(String whatever, int binary) { Random random = new Random(System.nanoTime()); boolean gamble = random.nextBoolean(); System.err.println( "\n&gt;&gt;&gt; GAMBLE CALLED &lt;&lt;&lt;\n" ); PowerMock_Test.gambleCalled = true; return gamble; } } </code></pre> <p>^ understandably, since my workspace does not support JNDI, only the production environment does</p> <p>% I am using the latest versions of all the library, JUnit 4.10, Mockito 1.8.5, Hamcrest 1.1, Javassist 3.15.0, and PowerMock 1.4.10.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload