exception: org.mockito.exceptions.verification.WantedButNotInvoked when calling verifyStatic. Using maven, surefire, testng, mockito

1,076 views
Skip to first unread message

Yan

unread,
Aug 27, 2013, 3:08:52 PM8/27/13
to powe...@googlegroups.com
Hi!  I got org.mockito.exceptions.verification.WantedButNotInvoked exception when I tried to verify a static method.

Here is the class under test:
public class ResourceNameHelper {
    public static String getNormalizedResourceNamespace(String namespace) {
        return (namespace.endsWith("/")) ? namespace : namespace + "/";
    }
}

Here is the test:
@PrepareForTest(ResourceNameHelper.class)
public class ResourceNameHelperTest  extends PowerMockTestCase {
   
    @Test
    public void testStringEndingWithSlash() {
        PowerMockito.mockStatic(ResourceNameHelper.class);
        PowerMockito.when(ResourceNameHelper.getNormalizedResourceNamespace("dataset")).thenReturn("dataset/");

        PowerMockito.verifyStatic();
        ResourceNameHelper.getNormalizedResourceNamespace("dataset");  
       
    }
}

Maven dependency:
        <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-testng</artifactId>
                <version>1.4.12</version>
                <scope>test</scope>
        </dependency>

        <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-testng-agent</artifactId>
                <version>1.4.12</version>
        </dependency>

        <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito</artifactId>
                <version>1.4.12</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.2</version>
            <scope>compile</scope>
        </dependency>

using surefire 2.10.


Here is the stacktrace:
Actually, there were zero interactions with this mock.

        at org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:292)

        at org.powermock.api.mockito.internal.invocationcontrol.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:194)

        at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)

        at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)

        at ResourceNameHelper.getNormalizedResourceNamespace(ResourceNameHelper.java)

        at ResourceNameHelperTest.testStringEndingWithSlash(ResourceNameHelperTest.java:21)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)

        at org.testng.internal.Invoker.invokeMethod(Invoker.java:691)

        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883)

        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1208)

        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)

        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)

        at org.testng.TestRunner.privateRun(TestRunner.java:754)

        at org.testng.TestRunner.run(TestRunner.java:614)

        at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)

        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)

        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)

        at org.testng.SuiteRunner.run(SuiteRunner.java:241)

        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)

        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)

        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1169)

        at org.testng.TestNG.runSuitesLocally(TestNG.java:1094)

        at org.testng.TestNG.run(TestNG.java:1006)

        at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:70)

        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:109)

        at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:111)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)

        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)

        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)

        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:81)

        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)


Thanks in advance!
Yan

Johan Haleby

unread,
Aug 28, 2013, 1:39:54 AM8/28/13
to powe...@googlegroups.com
That looks alright to me, you've never called the static method. Change it to:

  PowerMockito.mockStatic(ResourceNameHelper.class);
  PowerMockito.when(ResourceNameHelper.getNormalizedResourceNamespace("dataset")).thenReturn("dataset/");

  ResourceNameHelper.getNormalizedResourceNamespace("dataset")

  PowerMockito.verifyStatic();
  ResourceNameHelper.getNormalizedResourceNamespace("dataset");   

And see if it works.

Regards
/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages