当用于使用静态方法调用来测试Java类时,GroovyMock不起作用

时间:2016-10-10 14:09:22

标签: java unit-testing groovy mocking spock

当在其他Java类中使用该类时,Groovy测试不会为静态类创建模拟。以下是证明此内容的代码段

正在测试的Java类:

public class JavaClass {
    public void method() { 
      ClassWithStaticMethod.doSomething();
    }
}

使用静态方法的Java类:

public class ClassWithStaticMethod { 
    public static void doSomething() {
        System.out.println("Static method called");
    } 
}

失败的Groovy测试:

class JavaClassTest extends Specification {
  def 'test'() {
    given:
    GroovyMock(ClassWithStaticMethod, global: true)
    JavaClass javaClass = new JavaClass()

    when:
    javaClass.method()

    then:
    1 * ClassWithStaticMethod.doSomething() // <--- FAILS
  }
}

此操作失败并显示消息:

Static method called <--- original method is called, it's not mocked

Too few invocations for:

1 * ClassWithStaticMethod.doSomething()   (0 invocations)

Unmatched invocations (ordered by similarity):

None

因此,静态方法不会被模拟,并且总是调用实际的实现。谁能解释这种行为?知道如何绕过这个吗?

Java版本:1.7.0_79,Groovy版本:2.4.7,Spock版本:1.0-groovy-2.4,cgclib:3.1

1 个答案:

答案 0 :(得分:0)

如果使用groovyc

编译java代码
    def "foo"() {
      setup:
        // GroovyMock(ClassWithStaticMethod, global: true)
        GroovySpy(ClassWithStaticMethod, global: true)
        JavaClass javaClass = new JavaClass()

      when:
        javaClass.method()

      then:
        // 1 * ClassWithStaticMethod.doSomething()
        1 * ClassWithStaticMethod.doSomething() >> null
    }

如果java代码与javac

进行复合

1.prepare测试代码(此示例为MySpock.groovy)

// This Grabs are used for compile.
// These libraries must specifiy on class path when the file executs.
@Grapes([
  @Grab(group='org.jmockit', module='jmockit', version='1.8'),
  @Grab(group='org.spockframework', module='spock-core', version='1.0-groovy-2.4')
])

import spock.lang.*
import mockit.*

class MySpock extends Specification {

  def "hoge"() {
    setup:

    // Overwrite ClassWithStaticMethod#doSomething(static method) with JMockit/MockUp
    new MockUp<ClassWithStaticMethod>() {
      @Mock
      public static void doSomething() {
        Logger.append("abc")
      }

    }

    // Target object for this test
    JavaClass javaClass = new JavaClass()

    when: "Execute target method"
    javaClass.method()

    then: "ClassWithStaticMethod#doSomething was mocked and write the value in a file"
    Logger.count() == 1

    cleanup: "Delete log file for this test"
    Logger.delete()
  }
}

// Logging Utility for only this test.
class Logger {
  static logFile = new File("/tmp/MySpock.groovy.log")
  static append = {String msg -> logFile.append("${msg}${System.getProperty("line.separator")}")}
  static count = {logFile.readLines()?.size()?:0}
  static delete = { logFile.delete() }
}

2.compile codes

javac ClassWithStaticMethod.java&&javac JavaClass.java&&groovyc MySpock.groovy

3.执行测试

groovy -cp .:$HOME/.groovy/grapes/org.jmockit/jmockit/jars/jmockit-1.8.jar:$HOME/.groovy/grapes/org.spockframework/spock-core/jars/spock-core-1.0-groovy-2.4.jar MySpock

我第一次使用JMockit 所以,我不知道这种用法是否正确 我不知道在Spock和Mock(JMockit)之间访问和添加staic字段的方法。
因此我创建了日志文件以检查&#34; ClassWithStaticMethod#doSomething&#34;从&#34; JavaClass#method&#34;。

调用