无法 运行 Ktor 应用程序中的入门级 Spek 测试

Cannot run the starter Spek test within a Ktor application

我正在使用 IntellJ 2017.2.5 版构建 Ktor 应用程序。我还想使用 Spek 来测试应用程序。我从一个非常简单的开始,取自文档:

import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
import kotlin.test.assertEquals

class SampleSpec : Spek({
    given("some context") {
        on("executing some action") {
            it("should pass") {
                assertEquals(2, 2)
            }
        }
    }
})

当我右键单击 运行 测试时,出现以下错误:

Feb 08, 2018 2:50:32 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'spek' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/platform/engine/discovery/ClasspathSelector
    at org.jetbrains.spek.engine.SpekTestEngine.resolveSpecs(SpekTestEngine.kt:48)
    at org.jetbrains.spek.engine.SpekTestEngine.discover(SpekTestEngine.kt:36)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at org.jetbrains.spek.tooling.runner.junit.JUnitPlatformSpekRunner.run(JUnitPlatformSpekRunner.kt:107)
    at org.jetbrains.spek.tooling.MainKt.main(Main.kt:58)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.discovery.ClasspathSelector
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

抱歉冗长 build.gradle,但这里是:

group 'MyApp'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.2.21'
    ext.ktor_version = '0.9.1'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        //testing
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4'
        testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
        testCompile ('org.jetbrains.spek:spek-api:1.1.5') {
            exclude group: 'org.jetbrains.kotlin'
        }
        testRuntime ('org.jetbrains.spek:spek-junit-platform-engine:1.1.5') {
            exclude group: 'org.junit.platform'
            exclude group: 'org.jetbrains.kotlin'
        }
    }
}

//testing
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'java'
apply plugin: 'kotlin'

junitPlatform {
    platformVersion '1.0.0-M4'
    filters {
        engines {
            include 'spek'
        }
    }
}
sourceCompatibility = 1.8

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

kotlin {
    experimental {
        coroutines "enable"
    }
}

repositories {
    mavenCentral()
    maven { url "https://dl.bintray.com/kotlin/kotlinx" }
    maven { url "https://dl.bintray.com/kotlin/ktor" }
    maven { url "https://dl.bintray.com/jetbrains/spek" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "io.ktor:ktor-server-netty:$ktor_version"
    //testing
    testCompile 'org.jetbrains.kotlin:kotlin-test:1.1.0'
    testCompile 'org.jetbrains.spek:spek-api:1.1.4'
    testCompile 'org.jetbrains.spek:spek-junit-platform-engine:1.0.89'
    testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.0.0-M4'
}

知道问题出在哪里吗?

您使用的 Spek 和 Junit 平台版本不兼容。您应该更新以使用 Spek 文档推荐的版本:http://spekframework.org/docs/latest/#_gradle

我已将您的示例更新为使用 Spek 1.1.5 和 Junit Platform 1.0.0

group 'MyApp'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.2.21'
    ext.ktor_version = '0.9.1'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
    }
}

//testing
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'java'
apply plugin: 'kotlin'

junitPlatform {
    filters {
        engines {
            include 'spek'
        }
    }
}
sourceCompatibility = 1.8

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

kotlin {
    experimental {
        coroutines "enable"
    }
}

repositories {
    mavenCentral()
    maven { url "https://dl.bintray.com/kotlin/kotlinx" }
    maven { url "https://dl.bintray.com/kotlin/ktor" }
    maven { url "https://dl.bintray.com/jetbrains/spek" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "io.ktor:ktor-server-netty:$ktor_version"
    //testing
    testCompile 'org.jetbrains.kotlin:kotlin-test:1.1.0'
    testCompile 'org.jetbrains.spek:spek-api:1.1.5'
    testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
    testRuntime 'org.junit.platform:junit-platform-launcher:1.0.0'
}

我还发现包含 testRuntime 'org.junit.platform:junit-platform-launcher:1.0.0' 有助于确保 IntelliJ 插件使用正确的 Junit 版本。