2015-04-01 36 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所說,你應該包含靜態方法來模擬這個類,它不應該是測試類本身......