java - TestNG and Powermockito - cannot mock static void -
i can't powermock work testng. same code adjusted junit works fine, somehow in testng fails. testng version 6.8.21 powermockito version 1.6.1
package p; import org.mockito.mockitoannotations; import org.powermock.api.mockito.powermockito; import org.powermock.core.classloader.annotations.preparefortest; import org.powermock.modules.junit4.powermockrunner; import org.testng.assert; import org.testng.annotations.beforetest; import org.testng.annotations.test; import static org.powermock.api.mockito.powermockito.mockstatic; @preparefortest({footest.class}) public class footest { @beforetest public void setup() { mockstatic(footest.class); mockitoannotations.initmocks(this); } @test public void test() throws exception { powermockito.donothing().when(footest.class,"foo"); footest.foo(); assert.assertequals(1, 1); } public static void foo() throws exception { throw new exception("huh?"); } }
the exception on when method:
java.lang.exception: huh? @ p.footest.foo(footest.java:32) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.powermock.reflect.internal.whiteboximpl.performmethodinvocation(whiteboximpl.java:1873) @ org.powermock.reflect.internal.whiteboximpl.doinvokemethod(whiteboximpl.java:773) @ org.powermock.reflect.internal.whiteboximpl.invokemethod(whiteboximpl.java:753) @ org.powermock.reflect.whitebox.invokemethod(whitebox.java:466) @ org.powermock.api.mockito.internal.expectation.powermockitostubberimpl.when(powermockitostubberimpl.java:106) @ p.footest.test(footest.java:25) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.testng.internal.methodinvocationhelper.invokemethod(methodinvocationhelper.java:85) @ org.testng.internal.invoker.invokemethod(invoker.java:696) @ org.testng.internal.invoker.invoketestmethod(invoker.java:882) @ org.testng.internal.invoker.invoketestmethods(invoker.java:1189) @ org.testng.internal.testmethodworker.invoketestmethods(testmethodworker.java:124) @ org.testng.internal.testmethodworker.run(testmethodworker.java:108) @ org.testng.testrunner.privaterun(testrunner.java:767) @ org.testng.testrunner.run(testrunner.java:617) @ org.testng.suiterunner.runtest(suiterunner.java:348) @ org.testng.suiterunner.runsequentially(suiterunner.java:343) @ org.testng.suiterunner.privaterun(suiterunner.java:305) @ org.testng.suiterunner.run(suiterunner.java:254) @ org.testng.suiterunnerworker.runsuite(suiterunnerworker.java:52) @ org.testng.suiterunnerworker.run(suiterunnerworker.java:86) @ org.testng.testng.runsuitessequentially(testng.java:1224) @ org.testng.testng.runsuiteslocally(testng.java:1149) @ org.testng.testng.run(testng.java:1057) @ org.testng.remote.remotetestng.run(remotetestng.java:111) @ org.testng.remote.remotetestng.initandrun(remotetestng.java:204) @ org.testng.remote.remotetestng.main(remotetestng.java:175) @ org.testng.remotetestngstarter.main(remotetestngstarter.java:125) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ com.intellij.rt.execution.application.appmain.main(appmain.java:134) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ com.intellij.rt.execution.commandlinewrapper.main(commandlinewrapper.java:130) =============================================== custom suite total tests run: 1, failures: 1, skips: 0 ===============================================
so missing here?
i suspect may same issue , solution this question. in other words make sure using org.powermock.modules.testng.powermockobjectfactory
. @fge says, should @preparefortest
class containing static method(s) mock, should not test class itself...
Comments
Post a Comment