1

I'm trying to write a spock test with static fields (as expected output) that I define in the setupSpec(). I also want to setup my mocks and the expected return values in this method.

So I started with something like this:

package spocktest

import org.junit.ClassRule
import org.junit.Rule
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.rule.PowerMockRule
import spock.lang.Shared

@PrepareForTest(StaticFoo)
class StaticInitTest extends spock.lang.Specification {
    @Rule PowerMockRule instanceRule = new PowerMockRule();

    @Shared static Foo FOO1

    def setupSpec() {
        FOO1 = Stub(Foo)

        PowerMockito.spy(StaticFoo)
        PowerMockito.doReturn(true).when(StaticFoo, "x")
    }

    def "test1"() {
        given:
        when:
        def x = StaticFoo.x()
        then:
        println FOO1
        x
    }

    class Foo {}

    class StaticFoo {
        static boolean x() { false }
    }
}

There are two problems with this: FOO1 is null and x is false (so PowerMockito did not intercept)


I then changed

@Rule PowerMockRule instanceRule = new PowerMockRule();

to

@ClassRule static PowerMockRule rule = new PowerMockRule();

Now FOO1 is the Stub defined in setupSpec(), but x is still false (again, PowerMockito did not work)


Still having the @ClassRule, I moved

PowerMockito.spy(StaticFoo)
PowerMockito.doReturn(true).when(StaticFoo, "x")

into the when:block, which also did not work. Again x was false.


I changed @ClassRule back to @Rule and PowerMockito finally worked when defining behaviour in the when: block

x is true, but FOO1 is null


When I define both @ClassRule and @Rule, only the @Rule seems to be active (FOO1 is null, x is true)

Apparently, I can either have one or the other working.


Now, I have two questions:

  • How can I use both static fields and PowerMockito? What am I missing?
  • Is it possible to setup the expected return values in a setupSpec() (or any other) method outside the actual test?

Versions used:

+- junit:junit:jar:4.11:test
|  \- org.hamcrest:hamcrest-core:jar:1.3:test
+- org.mockito:mockito-all:jar:1.9.5:test
+- org.spockframework:spock-core:jar:1.0-groovy-2.4:test
|  \- org.codehaus.groovy:groovy-all:jar:2.4.4:test
+- cglib:cglib-nodep:jar:2.2.2:test
+- org.objenesis:objenesis:jar:2.1:test
+- com.athaydes:spock-reports:jar:1.2.7:test
+- org.powermock:powermock-module-junit4:jar:1.5.3:test
|  \- org.powermock:powermock-module-junit4-common:jar:1.5.3:test
|     \- org.powermock:powermock-reflect:jar:1.5.3:test
+- org.powermock:powermock-api-mockito:jar:1.5.3:test
|  \- org.powermock:powermock-api-support:jar:1.5.3:test
+- org.powermock:powermock-module-junit4-rule:jar:1.5.1:test
|  +- org.powermock:powermock-classloading-base:jar:1.5.1:test
|  \- org.powermock:powermock-core:jar:1.5.1:test
\- org.powermock:powermock-classloading-xstream:jar:1.5.1:test
tim_yates
  • 167,322
  • 27
  • 342
  • 338
Sebastian Saip
  • 452
  • 2
  • 5
  • 17
  • Why are you using PowerMock? What does it give you over Spock's Mocks and Spies? – tim_yates May 25 '16 at 13:42
  • the ability to mock static methods in existing Java classes, see also [this answer](http://stackoverflow.com/a/15834010/1239103) – Sebastian Saip May 25 '16 at 13:47
  • this may help: https://github.com/kriegaex/Spock_PowerMock/blob/master/src/test/groovy/de/scrum_master/spock_powermock/PersonTest.groovy – zack Aug 04 '16 at 17:02

0 Answers0