2013-10-22 97 views
2
RunWith(PowerMockRunner.class) 
@PrepareForTest(StaticCallInvoke.class) 
@ContextConfiguration(locations = "file:test/spring/Beans.xml") 
class TestClass extends Specification{ 
@Test 
    def "Testing staticMocking"() { 


     setup: 
     def someObject=new SomeObject(); 
     someObject.someValue=100 
     PowerMockito.mockStatic(StaticCallInvoke.class) 

     when: 
     ClassUnderTest.executeSomething(someObject) 

     then: 
     someObject.someValue=110 /*Wrong Value,It says assertion failed. Thats absolutely fine becuase the value should be 100*/ 

    } 
} 

當我嘗試將其更改爲正確的值,即100,它拋出此異常,斯波克行爲古怪

java.lang.NullPointerException: Cannot invoke method leaveScope() on null object 
    at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77) 
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42) 
    at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112) 
    at com.cognizant.awcoe.gamification.rules.helper.executors.RuleExecTest.$spock_feature_0_0(RuleExecTest.groovy:54) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310) 
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86) 
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282) 
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84) 
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) 
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) 
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118) 
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101) 
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) 
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)) 

如果我改回100,這一切都再好的(但斷言失敗)

以上測試使用PowerMock 1.5,Spock-0.6 for groovy 1.8來運行。 不確定這裏發生了什麼事。

任何幫助是非常讚賞:)

回答

2

貌似PowerMock是安裝了自己的JUnit運行,覆蓋Spock的一個。換句話說,Spock不再負責執行這個測試,PowerMock顯然不能正確執行它。也許PowerMock不支持自定義的JUnit跑步者,在這種情況下,它不適用於Spock。

+0

感謝您的回覆。除了powermock(也許,Jmockito?)之外的任何建議都可以解決我的問題嗎?或者是代碼重新編寫更好的方法? – Avinash

+1

還沒有看過這麼多,但並沒有Spock有它自己的嘲弄功能(哇,聽起來:P) – justin

+0

現在你已經編輯你的問題,它表明你正在應用'RunWith(PowerMockRunner.class)你自己。這將無法正常工作,因爲它會覆蓋Spock的跑步者。據我所知,'PowerMockRunner'不是強制性的,因此我建議不要這樣做。 –

1

如果做得對,Spock和PowerMock很好地協作,請參閱我的回購https://github.com/kriegaex/Spock_PowerMock。您需要做類似

@PrepareForTest([StaticCallInvoke.class]) 
@ContextConfiguration(locations = "file:test/spring/Beans.xml") 
class TestClass extends Specification { 
    @Rule PowerMockRule rule = new PowerMockRule(); 

    def "Testing staticMocking"() { 
     // ... 
    } 
} 
+1

嘗試執行此示例時出現以下錯誤。唯一的補充是,我使用的是RoboSputnik跑步者。 com.thoughtworks.xstream.converters.ConversionException:無法調用org.apache.tools.ant.Project $ AntRefTable.writeObject():org/fusesource/jansi/Ansi ----調試信息---- 消息:無法調用org.apache.tools.ant.Project $ AntRefTable.writeObject() cause-exception:java.lang.NoClassDefFoundError cause-message:org/fusesource/jansi/Ansi – BenDroid