Using PowerMock with Spock

I have a class with several static methods. I need to mock these static methods. I know that PowerMock does this, but I could not find any tutorials / materials that shed light on the integration of "Spock + PowerMock". I prefer Spock Junt, hence the puzzle. Is there any way to get these 2 frameworks to play ball? Any help is greatly appreciated. Apply the code, especially.

Update: Current Approach Status

Spock is acting weird

+9
powermock groovy spock
source share
3 answers

I'm stuck here too. After searching for several hours, I saw this github repo: https://github.com/kriegaex/Spock_PowerMock .

I tried adding PowerMockRule, which essentially allowed me to use PowerMock with Spock. I had to add these dependencies. Version 1.5.4

<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4-rule</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-classloading-xstream</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> 

My class is as follows:

 import org.junit.Rule import org.mockito.Mockito import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.rule.PowerMockRule import spock.lang.Specification @PrepareForTest([SomeStaticClass.class]) public class FlightFormSpec extends Specification { @Rule PowerMockRule powerMockRule = new PowerMockRule(); def "When mocking static"() { setup : PowerMockito.mockStatic(SomeStaticClass.class) when : Mockito.when(SomeStaticClass.someStaticMethod()).thenReturn("Philippines!"); then : SomeStaticClass.someStaticMethod() == "Philippines!" } } 

Here is another resource: https://github.com/jayway/powermock/wiki/powermockrule

+14
source share

No special integration; it’s best to try using PowerMock β€œas is”. From what I remember, PowerMock had problems with Groovy, and I don't know if this was allowed. And if I'm not mistaken, PowerMock overwrites the byte code of the test classes, so the next question is whether it works with Spock. Let us know what you find.

+1
source share

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> 
0
source share

All Articles