Search code examples
javamockitotestngpowermockpowermockito

PowerMock and Mockito UnfinishedVerificationException


I am having difficulties getting PowerMock and Mockito to work.

I am using these versions. They should be compatible.

  • powermock-core v 1.7.4
  • powermock-api-mockito v 1.7.4
  • powermock-module-junit4 v 1.7.4
  • mockito-all v 1.10.19

I am also using TestNG, but I do not believe this has any affect on the outcome of this test.

I want to mock an object, call a method on the mocked object, and verify that a private method was called once within the mocked objects method.

I have constructed an example, that hopefully explains what I want to accomplish.

public class MyClass {

    public void execute(Object o) {
        valid(o);
    }

    private boolean valid(Object o) {
        return o != null;
    }
}

This is my test class

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class TestClass {
    @After
    public void validate() {
        validateMockitoUsage();
    }

    @Test
    public void executeTest() throws Exception {
        //Initialize the Class and create the spy
        MyClass myClass = PowerMockito.spy(new MyClass());

        //Execute the method which I wish to test
        myClass.execute(null);

        //I want to verify that the private valid method was called once.
        PowerMockito.verifyPrivate(myClass, times(1)).invoke("valid", anyObject());
    }
}

I get the following error:

org.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:
-> at TestClass.executeTest(MyClass.java:14)

Example of correct verification:
    verify(mock).doSomething()

Also, this error might show up because you verify either of: 

final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.


at TestClass.validate(MyClass.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

I have tried to search, but I could not find a solution for my problem. I believe this is a common error, and I am using PowerMock/Mockito wrong.


Solution

  • As you are using TestNG to run your test, the @RunWith annotation is ignored as it is specific to JUnit.

    To make this work, you have 2 options:

    1. Run your test with JUnit.

    2. OR Configure TestNG so that it uses PowerMock object factory. The configuration details are pretty well explained here.