使用私有方法的Grails spock测试在升级到2.5.0后失败

Sha*_*wal 9 grails spock

我们已将我们的应用程序从Grails 2.4.4升级到2.5.0.一切正常,但有些测试用例失败,其中包含一些私有方法.

import org.junit.Before

class UserServiceSpec extends IntegrationSpec {

    @Before
    def setup() {
         // Some initialization
    }

    // being used in other test methods
    private void getParams() {
          // some code
    }

    void testUserUniqueEmali() {
         when: "this runs"
         // code
         then: "hello"
         // code
    }
}
Run Code Online (Sandbox Code Playgroud)

抛出异常:

java.lang.IllegalAccessException: Class org.spockframework.util.ReflectionUtil can not access a member of class com.test.UserServiceSpec with modifiers "private"
    at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:153)
    at org.spockframework.runtime.model.MethodInfo.invoke(MethodInfo.java:84)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at _GrailsTest.run_closure1(_GrailsTest.groovy:102)
    at TestApp.run_closure1(TestApp.groovy:32)
Run Code Online (Sandbox Code Playgroud)

我生成了依赖性报告,这里有一些统计数据:

org.spockframework:spock-core:1.0-groovy-2.4
org.codehaus.groovy:groovy-all:2.4.3
Run Code Online (Sandbox Code Playgroud)

这表明正在使用最新版本的Spock框架,但我无法解决这个问题.我已经尝试删除"私人"修饰符,但它仍然无法正常工作.

Sha*_*wal 10

经过一段时间的调查,我发现了问题,但没有找到根本原因.

基本上,这个@Before注释是罪魁祸首,由于某种原因,spock测试抛出了这个错误.删除未使用的注释修复了我的问题.

这在Grails 2.4.4中工作并且在2.5.0中开始失败,可能是因为Spock框架的变化.


ZZ *_*Z 5 5

我遇到了同样的问题。为了防止它们,只是不要命名带有JUnit注释的方法@Before@After类似Spock的方法:清理和设置。