当我尝试使用 Gradle 和 Cucumber 构建测试时出错

Error when I try to build tests with Gradle and Cucumber

我正在尝试使用 Cucumber 库编写功能测试,不幸的是,我不能同时使用 Cucumber 和 Gradle,构建测试时发生错误。我是 Java 环境中的真正初学者。我在这个项目中使用:Kotlin / Gradle / Cucumber.

这是我的 build.gradle.kts 的样子

import org.gradle.jvm.tasks.Jar

plugins {
    application
    `java-library`
    `maven-publish`
    signing
    jacoco
    kotlin("jvm") version "1.3.61"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib"))

    testImplementation("org.mock-server:mockserver-netty:5.3.0")
    testImplementation("io.cucumber:cucumber-java8:7.0.0")
    testImplementation("io.cucumber:cucumber-junit:7.0.0")
}

tasks.register<Jar>("fatJar") {
    archiveFileName.set("${artifactName}-${artifactVersion}.jar")
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
      attributes("Main-Class" to application.mainClassName)
    }
    from(configurations.runtimeClasspath.get()
        .onEach { println("Add from dependencies: ${it.name}") }
        .map { if (it.isDirectory) it else zipTree(it) })
    val sourcesMain = sourceSets.main.get()
    sourcesMain.allSource.forEach { println("Add from sources: ${it.name}") }
    from(sourcesMain.output)
}

tasks.register<Jar>("sourcesJar") {
    from(sourceSets.main.get().allJava)
    archiveClassifier.set("sources")
}

tasks.register("cucumber") {
     dependsOn compileTestKotlin
     doLast {
         javaexec {
             main = "io.cucumber.core.cli.Main"
             classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
         }
     }
}

这里是错误

* What went wrong:
Script compilation errors:

  Line 104:      dependsOn compileTestKotlin
                           ^ Unresolved reference: compileTestKotlin

  Line 108:              classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
                                                    ^ Unresolved reference: cucumberRuntime

  Line 108:              classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
                                                                                      ^ Unresolved reference: output

  Line 108:              classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
                                                                                                               ^ Unresolved reference: output

4 errors

感谢您的帮助。

您似乎正在尝试从 Groovy DSL 转换为 Kotlin。请记住,在 Kotlin DSL 中,一切都是强类型的,因为 Kotlin 是强类型的。

您需要将所有任务创建包装在 tasks { } 块中才能访问 compileTestKotlin。目前,cucumber 任务注册中 this 的范围是 DefaultTask 类型的任务本身,它不知道 compileTestKotlin.

另外:

  • mainClassName 已弃用,应改用 mainClass
  • java 扩展提供了为您创建源 JAR 的能力,因此您无需自己创建任务,除非您对 JAR 有特定要求,而您的代码片段似乎并非如此。
  • 由于 cucumber 任务的目标是执行 Java main class,您可以使用 JavaExec 作为任务类型而不是 DefaultTask

抛光样品(未测试):

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.5.31"
    application
}

repositories {
    mavenCentral()
}

configurations {
    register("cucumberRuntime") {
        extendsFrom(testImplementation.get())
    }
}

dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    testImplementation(platform("io.cucumber:cucumber-bom:7.2.3"))
    testImplementation("io.cucumber:cucumber-java8")
    testImplementation("io.cucumber:cucumber-junit")
}

java {
    withSourcesJar()
}

application {
    mainClass.set("io.mateo.ExampleKt")
}

tasks {
    register<Jar>("fatJar") {
        archiveFileName.set("foo-bar.jar")
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        manifest {
            attributes("Main-Class" to application.mainClass.get())
        }
        from(configurations.runtimeClasspath.get()
                .onEach { println("Add from dependencies: ${it.name}") }
                .map { if (it.isDirectory) it else zipTree(it) })
        val sourcesMain = sourceSets.main.get()
        sourcesMain.allSource.forEach { println("Add from sources: ${it.name}") }
        from(sourcesMain.output)
    }
    register<JavaExec>("cucumber") {
        dependsOn(compileTestKotlin)
        mainClass.set("io.cucumber.core.cli.Main")
        classpath = configurations["cucumberRuntime"] + sourceSets.main.get().output + sourceSets.test.get().output
    }
}

参考文献: