30

I am trying 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 sample code from another questions answer, and while the test passes, it seems that the underlying method still gets called. I inserted a System.err.println() in the doTheGamble() method, and it gets printed out to my console.

Interesting enough, if I comment out the first assertThat, the test passes. ?:(

So, how do I mock out a private method so that it does not get called?

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 << 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>>> GAMBLE CALLED <<<\n" );
        PowerMock_Test.gambleCalled = true;

        return gamble;
    }
}   

^ understandably, since my workspace does not support JNDI, only the production environment does

% 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.

Ruchika Sharma
  • 648
  • 1
  • 7
  • 23
Sled
  • 18,541
  • 27
  • 119
  • 168

5 Answers5

37

From the PowerMock Private Method Example:

@RunWith(PowerMockRunner.class)
// We prepare PartialMockClass for test because it's final or we need to mock private or static methods
@PrepareForTest(PartialMockClass.class)
public class YourTestCase {
@Test
public void privatePartialMockingWithPowerMock() {        
    PartialMockClass classUnderTest = PowerMockito.spy(new PartialMockClass());

    // use PowerMockito to set up your expectation
    PowerMockito.doReturn(value).when(classUnderTest, "methodToMock", "parameter1");

    // execute your test
    classUnderTest.execute();

    // Use PowerMockito.verify() to verify result
    PowerMockito.verifyPrivate(classUnderTest, times(2)).invoke("methodToMock", "parameter1");
}

So to apply this to your code, I think it might become:

@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class PowerMock_Test {
    @Test(expected = RuntimeException.class)
    public void when_gambling_is_true_then_always_explode() throws Exception {
        CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod());

        PowerMockito.doReturn(true).when(spy, "doTheGamble", anyString(), anyInt());


/* 1 */ PowerMockito.verifyPrivate(spy, times(0)).invoke("doTheGamble", anyString(), anyInt());            
        spy.meaningfulPublicApi();
/* 2 */ PowerMockito.verifyPrivate(spy, times(2)).invoke("doTheGamble", anyString(), anyInt());            
    }
}

I just coded that in the editor here. No tests have actually been run, and no bugs have been harmed in the crafting of this code.

Mike
  • 7,994
  • 5
  • 35
  • 44
  • @Mike Do you have any idea why `doReturn(true).when(spy, "doTheGamble", anyString(), anyInt());` works but `doReturn( true ).when( spy, method( CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class ) ).withArguments( anyString(), anyInt() ) ;` results in a `IllegalArgumentException: argument type mismatch` ? The latter seems more in keeping with the style of the example and at least equivalent but does not work. – Sled Nov 09 '11 at 16:04
  • I honestly cannot, sorry. I'm relatively new to Mockito/PowerMock myself... I've never attempted writing code in that style (`.withArguments(...)`) before. – Mike Nov 09 '11 at 16:31
  • My totally unsubstantiated guess is you are trying to match an `int` with an `Integer`... – Christian Sep 23 '15 at 16:28
  • I've tried this with .withArguments with different objects but it just doesnt seem to work... PowerMockito.doReturn(true).when(spy, "doTheGamble", anyString(), anyInt()); works like a charm! – bram000 Jul 07 '17 at 10:30
  • @Christian - Could you please guide on https://stackoverflow.com/questions/52064126/powermock-emma-code-coverage-showing-0-for-private-static-methods-and-other. How we can ensure 100% code coverage ? – Jeff Cook Aug 28 '18 at 18:38
3

ArtB,

Just pasting the complete code which works fine in my Eclipse IDE. I have only changed the expectation i said in my last post. Good luck.

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.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());

//        PowerMockito.doReturn(true).when(spy, "doTheGamble", anyString(), anyInt());

        PowerMockito.doReturn(true).when(spy, 
               method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))
                .withArguments(anyString(), anyInt());

        assertThat( PowerMock_Test.gambleCalled, is(false) );
        spy.meaningfulPublicApi();
        assertThat( PowerMock_Test.gambleCalled, is(false) );
    }
}


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();

        System.err.println( "\n>>> GAMBLE CALLED <<<\n" );
        PowerMock_Test.gambleCalled = true;

        return gamble;
    }
}   
skusunam
  • 411
  • 3
  • 12
1

ArtB,

Are you sure your code doesn't work (or) am i missing something here? I just replaced your method expectation with the following one the way Mike suggested and it works fine:

PowerMockito.doReturn(true).when(spy, 
               method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))
                .withArguments(anyString(), anyInt());

I never used Powermockito but used Mockito a lot before.

skusunam
  • 411
  • 3
  • 12
0

None of the above worked for me because my private method was void.

I had a setup like:

public class MainClass {
    public void publicFunction () {
        this.privateFunction(Arg1.class arg1, Arg2.class arg2);
    }
        
    private void privateFunction(Arg1.class arg1, Arg2.class arg2){
        // who cares, I don't want any of this to go off
    }
}

and the only thing that worked was:

@Test
public void whocares() throws Exception {
    mainSpy = spy(mockedMainClassInstance);
    PowerMockito.doNothing().when(mainSpy, "privateFunction", mockedArg1, mockedArg2);
    mainSpy.publicFunction(mockedArg0, mockedArg1, mockedArg2);
    verifyPrivate(mainSpy).invoke("privateFunction", mockedArg1, mockedArg2);
}

and that's when the code in the private function stopped firing

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
blah blah
  • 425
  • 6
  • 12
0

when you use spy to construct a mock object, its a real half backed object. My guess would be to get rid of spy. Work on pure mock object.

Ravi Bhatt
  • 3,147
  • 19
  • 21
  • 1
    I'm trying to test the implementation of the class I am spying so this is not an option. – Sled Nov 09 '11 at 02:47