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*/

    }
}

When I try to change it to the correct value i.e 100, it throws this Exception,

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) )

If I change it back to 100, it's all good again(But the assertion fails)

Above test is run using PowerMock 1.5,Spock-0.6 for groovy 1.8. Not sure what's happening here.

Any help is much appreciated :)

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
Avinash
  • 347
  • 1
  • 4
  • 15

2 Answers2

2

Looks like PowerMock is installing its own JUnit runner, overriding Spock's one. In other words, Spock is no longer in charge of executing this test, and PowerMock obviously can't execute it correctly. Perhaps PowerMock doesn't support custom JUnit runners, in which case it won't work with Spock.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Thank you for the response. Any suggestions apart from powermock(perhaps,Jmockito ?) that would solve my problem? Or is a code re-write a better approach? – Avinash Oct 22 '13 at 05:50
  • 1
    Haven't looked into this much but doesn't Spock have it's own mocking features (wow that sounded off :P) – justin Oct 22 '13 at 12:27
  • Now that you've edited your question, it shows that you are applying `RunWith(PowerMockRunner.class)` yourself. This won't work as it overrides Spock's own runner. As far as I know, `PowerMockRunner` isn't mandatory, hence I recommend to try without it. – Peter Niederwieser Oct 23 '13 at 00:06
1

Spock and PowerMock work together nicely if done right, see my repo https://github.com/kriegaex/Spock_PowerMock. You need to do something like

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

    def "Testing staticMocking"() {
        // ...
    }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • 1
    I get the following error when trying to do this example. The only addition I have that I can may be of relevance is that I am using the RoboSputnik runner. com.thoughtworks.xstream.converters.ConversionException: Could not call org.apache.tools.ant.Project$AntRefTable.writeObject() : org/fusesource/jansi/Ansi ---- Debugging information ---- message : Could not call org.apache.tools.ant.Project$AntRefTable.writeObject() cause-exception : java.lang.NoClassDefFoundError cause-message : org/fusesource/jansi/Ansi – BenDroid Aug 13 '15 at 14:14