Gradle 抱怨缺少 "setupMethod" 初始集成测试结构

Gradle complains about missing "setupMethod" with initial integration test structure

我正在编写我的第一个 Gradle 插件。默认构建工作正常,包括少量单元测试。我现在正在尝试使用 nebula.test.

设置集成测试基础架构

我创建了一个空的集成测试 class,以及 build.gradle 中的设置来配置 "integTest" 任务和相关的源集和依赖项。我使用了网上的几个示例和 github 上的现有插件来指导我。

在我展示我的代码之前,这是我得到的错误:

groovy.lang.MissingPropertyException: No such property: setupMethod for class: org.spockframework.runtime.model.SpecInfo Possible solutions: setupMethods

这是我的 "build.gradle" 文件:

    buildscript {
        repositories {
            jcenter()
            mavenCentral()
        }
    }

    apply plugin: 'groovy'
    apply plugin: 'java-gradle-plugin'
    apply plugin: 'maven'

    repositories {
        mavenCentral()
        jcenter()
        maven { url "http://oss.sonatype.org/content/repositories/releases/" }
        maven { url "http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot" }
        maven { url "http://nexus.opendaylight.org/content/repositories/opendaylight.release" }
    }

    dependencies {
        compile "org.codehaus.groovy:groovy-all:2.3.9"
        compile gradleApi()
        compile "org.opendaylight.yangtools:yang-parser-impl:0.7.0-SNAPSHOT"
        compile "org.opendaylight.yangtools:binding-java-api-generator:0.7.0-SNAPSHOT"
        compile "org.opendaylight.yangtools:binding-generator-api:0.7.0-SNAPSHOT"
        compile "org.opendaylight.yangtools:binding-generator-impl:0.7.0-SNAPSHOT"
        compile "commons-io:commons-io:2.4"

        testCompile("org.spockframework:spock-core:1.0-groovy-2.3") {
            exclude group: "org.codehaus.groovy"
        }
    }

    sourceCompatibility = 1.7

    group = 'com.att.opnfv.yang'
    version = '1.0.0-SNAPSHOT'

    sourceSets {
        integTest {
            groovy.srcDir file("src/integTest/groovy")
            resources.srcDir file("src/integTest/resources")
        }
    }

    dependencies {
        integTestCompile sourceSets.main.output
        integTestCompile configurations.testCompile
        integTestCompile sourceSets.test.output
        integTestRuntime configurations.testRuntime

        testCompile( 'com.netflix.nebula:nebula-test:2.2.0' ) {
            exclude module: 'groovy-all'
        }
    }

    task integTest(type: Test) {
        testClassesDir  = sourceSets.integTest.output.classesDir
        classpath     = sourceSets.integTest.runtimeClasspath
    }

    check.dependsOn -= integTest

这是我的简单集成测试规范(我在第一次尝试后添加了一个 "def",但没有任何区别):

import nebula.test.IntegrationSpec
class YangPluginIntegSpec extends IntegrationSpec {
    def 'simple'() {
        writeHelloWorld("com.example")
    }
}

更新:

这是错误的完整堆栈跟踪。

groovy.lang.MissingPropertyException: No such property: setupMethod for class: org.spockframework.runtime.model.SpecInfo Possible solutions: setupMethods at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:51) at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:63) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227) at com.energizedwork.spock.extensions.TempDirectoryInterceptor.install(TempDirectoryExtension.groovy:92) at com.energizedwork.spock.extensions.TempDirectoryInterceptor$install.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at com.energizedwork.spock.extensions.TempDirectoryExtension.visitFieldAnnotation(TempDirectoryExtension.groovy:21) at com.energizedwork.spock.extensions.TempDirectoryExtension.visitFieldAnnotation(TempDirectoryExtension.groovy) at org.spockframework.runtime.ExtensionRunner.doRunAnnotationDrivenExtensions(ExtensionRunner.java:100) at org.spockframework.runtime.ExtensionRunner.runAnnotationDrivenExtensions(ExtensionRunner.java:65) at org.spockframework.runtime.ExtensionRunner.runAnnotationDrivenExtensions(ExtensionRunner.java:60) at org.spockframework.runtime.ExtensionRunner.runAnnotationDrivenExtensions(ExtensionRunner.java:51) at org.spockframework.runtime.ExtensionRunner.run(ExtensionRunner.java:41) at org.spockframework.runtime.Sputnik.runExtensionsIfNecessary(Sputnik.java:88) at org.spockframework.runtime.Sputnik.getDescription(Sputnik.java:55) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:83) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) at com.sun.proxy.$Proxy2.processTestClass(Unknown Source) at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360) at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl.run(DefaultExecutorFactory.java:64) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744)

然后我尝试在调试器中逐步执行这段代码。我能够在第 227 行 AbstractCallSite.callGetProperty() 处命中断点。在我的测试用例中它命中此断点数十次。我还在第 63 行 GetEffectivePojoPropertySite.getProperty() 处设置了一个断点,但由于某种原因它从未达到该断点。

"callGetProperty()" 方法如下所示:

public Object callGetProperty(Object receiver) throws Throwable {
    return acceptGetProperty(receiver).getProperty(receiver); // 227
}

该方法如下所示:

public final Object getProperty(Object receiver) throws Throwable {
    try {
        return effective.getProperty(receiver); // line 61
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre); // line 63
    }
}

由于第 63 行在 catch 子句中,我也在第 61 行设置了一个断点,并且该断点也从未命中。

更新:

另一个有趣的地方是 TempDirectoryInterceptor.install() 调用堆栈跟踪的方法:

@Override
void install(SpecInfo spec) {
    spec.setupMethod.addInterceptor this // line 92
    spec.cleanupMethod.addInterceptor this
}

有 "setupMethod",但我不知道那应该是什么。此行上设置的断点未命中。

正如我在评论中所说,nebula.test 的最新版本需要 Spock 0.7。如果您还将 Spock 1.0 指定为依赖项,则会出现混乱。解决方法是删除对 Spock 1.0 的单独引用。

幸运的是,nebula.test 可能会很快升级为使用 Spock 1.0。以下 PR 请求待处理:https://github.com/nebula-plugins/nebula-test/pull/33 .