使用Spock的PowerMock

时间:2013-10-21 11:48:30

标签: groovy powermock spock

我有一个带有一些静态方法的类。我需要模拟这些静态方法。我知道PowerMock会这样做,但是我无法找到任何能够解释“Spock + PowerMock”集成的教程/材料。我更喜欢Spock和Junit,因此是一个难题。有没有办法让这两个框架发挥作用?任何帮助都非常感激。示例代码,更是如此。

更新:方法的当前状态

Spock behaving weirdly

3 个答案:

答案 0 :(得分:14)

我也被困在这里一段时间了。搜索了几个小时后,我看到了这个github回购:https://github.com/kriegaex/Spock_PowerMock

我尝试添加PowerMockRule,这实际上使我能够与Spock一起使用PowerMock。我不得不添加这些依赖项。版本为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>

我的班级看起来像这样:

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!"
    }
}

这是另一种资源:https://github.com/jayway/powermock/wiki/powermockrule

答案 1 :(得分:1)

没有特殊的整合;你最好的选择是尝试“按原样”使用PowerMock。根据我的记忆,PowerMock曾经遇到过Groovy的问题,我不知道这是否已经解决了。如果我没弄错的话,PowerMock会重写测试类的字节代码,所以下一个问题是它是否适用于Spock。让我们知道你找到了什么。

答案 2 :(得分:0)

从Powermock版本1.6.0开始,powermock允许委派测试运行程序。

这允许在PowerMock测试运行器框架中包装Spock测试运行器(Sputnik)。然后,Sputnik将启动测试用例规范,并仍然允许使用PowerMock框架。

通过JUnit4和Powermock,我使用以下模板访问静态类。

测试类:

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

依赖项

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