Starting with Powermock version 1.6.0, powermock allows you to delegate a test run.
This allows you to wrap a Spock test module (Sputnik) in a PowerMock test center environment. Sputnik will launch test case specifications and will still allow the use of the PowerMock platform.
In JUnit4 and Powermock, I use the following template to access static classes.
Test class:
package mypackage; import org.junit.runner.RunWith import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner import org.powermock.modules.junit4.PowerMockRunnerDelegate import org.spockframework.runtime.Sputnik import spock.lang.Specification @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(Sputnik.class) @PrepareForTest([MyStaticMethodClass.class]) class MyTestForClassTest extends Specification { def myStaticMethodClass def setup() { PowerMockito.mockStatic(MyStaticMethodClass.class) myStaticMethodClass= Mock(MyStaticMethodClass) PowerMockito.when(MyStaticMethodClass.getInstance()).thenReturn(myStaticMethodClass) } @Unroll def "#TestCase policy RF210 triggered"() { given: "a product list for the policy" myStaticMethodClass.someInstanceMethod() >> "my return value" classUnderTest = new ClassUnderTest() ...
Dependencies
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.7.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.7.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>1.3-groovy-2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>3.3.0</version> <scope>test</scope> </dependency>
sweetfa
source share