2015-04-01 83 views
0

我无法使用TestNG获取Powermock。调整为JUnit的代码工作正常,但在TestNG中以某种方式失败。 TestNG的版本是21年6月8日 Powermockito版本是1.6.1TestNG和Powermockito - 无法模拟静态无效

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?"); 
    } 
} 

我得到的例外是在当方法:

java.lang.Exception: huh? 
    at p.FooTest.foo(FooTest.java:32) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873) 
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773) 
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:753) 
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466) 
    at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:106) 
    at p.FooTest.test(FooTest.java:25) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:696) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:882) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1189) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) 
    at org.testng.TestRunner.privateRun(TestRunner.java:767) 
    at org.testng.TestRunner.run(TestRunner.java:617) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:348) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:254) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) 
    at org.testng.TestNG.run(TestNG.java:1057) 
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130) 
=============================================== 
Custom suite 
Total tests run: 1, Failures: 1, Skips: 0 
=============================================== 

那我在这里失踪?

+0

好的,除了你不应该在静态方法中嘲笑第一,我不确定你可以'@ PrepareForTest'你的测试类本身;你有没有尝试过,并将该方法放在另一个班级? – fge 2015-04-01 14:44:23

+0

@fge出于好奇,当需要出现时,嘲弄静态方法有什么不对?在一个真实世界的应用程序中,我最近不得不嘲笑其中的一些(在JSF'FacesContext'类中,以及JasperReports的一个类中);如果我不嘲笑他们,我还会写测试吗?为'FacesContext'和JasperReports创建不必要的包装器? – 2015-04-01 21:50:03

+0

@fge,当然了。这只是一个最小化的样本。顺便说一句,当我转换为JUnit时,它工作正常。 – 2015-04-07 11:22:00

回答

0

我怀疑这可能是与this question相同的问题和解决方案。换句话说,确保你使用的是org.powermock.modules.testng.PowerMockObjectFactory。但正如@fge所说,你应该包含静态方法来模拟这个类,它不应该是测试类本身......