使用 GroovyMock 或 Spock 中的类似方法模拟静态方法 [英] Mock static method with GroovyMock or similar in Spock

查看:15
本文介绍了使用 GroovyMock 或 Spock 中的类似方法模拟静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次来这里,如有遗漏,请见谅.我希望使用 Spock 绕过对静态方法的调用.反馈会很棒

First-timer here, apologies if I've missed anything. I'm hoping to get around a call to a static method using Spock. Feedback would be great

使用 groovy 模拟,我以为我可以通过静态调用,但还没有找到.作为背景,我正在对遗留 Java 中的测试进行改造.禁止重构.我正在使用 spock-0.7 和 groovy-1.8.

With groovy mocks, I thought I'd be able to get past the static call but haven't found it. For background, I'm in the process of retrofitting tests in legacy java. Refactoring is prohibited. I'm using spock-0.7 with groovy-1.8.

对静态方法的调用以这种形式与实例调用链接:

The call to the static method is chained with an instance call in this form:

public class ClassUnderTest{

public void methodUnderTest(Parameter param){
  //everything else commented out
Thing someThing = ClassWithStatic.staticMethodThatReturnsAnInstance().instanceMethod(param);
   }

}

staticMethod 返回一个 ClassWithStatic 的实例instanceMethod 返回方法其余部分所需的 Thing

staticMethod returns an instance of ClassWithStatic instanceMethod returns the Thing needed in the rest of the method

如果我直接执行全局模拟,它会返回模拟的实例:

If I directly exercise the global mock, it returns the mocked instance ok:

def exerciseTheStaticMock(){
    given:
    def globalMock = GroovyMock(ClassWithStatic,global: true)
    def instanceMock = Mock(ClassWithStatic)

    when:
    println(ClassWithStatic.staticMethodThatReturnsAnInstance().instanceMethod(testParam))

    then:
    interaction{
        1 * ClassWithStatic.staticMethodThatReturnsAnInstance() >> instanceMock
        1 * instanceMock.instanceMethod(_) >> returnThing
    }
}

但是如果我从 ClassUnderTest 运行 methodUnderTest:

But if I run the methodUnderTest from the ClassUnderTest:

def failingAttemptToGetPastStatic(){
    given:
    def globalMock = GroovyMock(ClassWithStatic,global: true)
    def instanceMock = Mock(ClassWithStatic)
    ClassUnderTest myClassUnderTest = new ClassUnderTest()

    when:
    myClassUnderTest.methodUnderTest(testParam)

    then:
    interaction{
        1 * ClassWithStatic.staticMethodThatReturnsAnInstance() >> instanceMock
        1 * instanceMock.instanceMethod(_) >> returnThing
    }
}

它抛出了一个真正的 ClassWithStatic 实例,该实例在其 instanceMethod 中继续失败.

It throws down a real instance of ClassWithStatic that goes on to fail in its instanceMethod.

推荐答案

Spock 只能模拟 Groovy 中实现的静态方法.对于在 Java 中实现的模拟静态方法,您需要使用类似 GroovyMockPowerMockJMockit.

Spock can only mock static methods implemented in Groovy. For mocking static methods implemented in Java, you'll need to use a tool like GroovyMock , PowerMock or JMockit.

PS:鉴于这些工具为了实现它们的目标而采用了一些深层次的技巧,我很想知道它们是否与在 Groovy/Spock(而不是 Java/JUnit)中实现的测试配合使用以及配合的效果如何.

PS: Given that these tools pull of some deep tricks in order to achieve their goals, I'd be interested to hear if and how well they work together with tests implemented in Groovy/Spock (rather than Java/JUnit).

这篇关于使用 GroovyMock 或 Spock 中的类似方法模拟静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆