我在kotlin测试中使用spek非常新.使用spek时,在logcat上出现以下错误.我不知道为什么我得到这个
java.lang.ClassNotFoundException: com.intellij.junit5.JUnit5IdeaTestRunner
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.junit.JUnitStarter.getAgentClass(JUnitStarter.java:252)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Run Code Online (Sandbox Code Playgroud)
我对gradle文件有以下依赖关系
testImplementation 'junit:junit:4.12'
testImplementation 'org.jetbrains.spek:spek-api:1.1.0-beta3'
testImplementation 'org.jetbrains.spek:spek-junit-platform-engine:1.1.0-beta3'
testImplementation 'org.junit.platform:junit-platform-runner:1.0.0-M3'
Run Code Online (Sandbox Code Playgroud)
测试文件
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.junit.Assert.assertEquals
import org.junit.platform.runner.JUnitPlatform
import org.junit.runner.RunWith
@RunWith(JUnitPlatform::class)
class ExampleUnitTest : Spek({
val x = 2
val y = 3
given("x = $x and y = $y") { …
Run Code Online (Sandbox Code Playgroud) 我很难配置基于JUnit Jupiter(5)的测试环境.那里有两个不同的错误:
WARNING: TestEngine with ID 'spek' failed to discover tests
org.junit.platform.commons.util.PreconditionViolationException: Could not load class with name...
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:61)...
Run Code Online (Sandbox Code Playgroud)
配置如下.
主要build.gradle
:
apply plugin: 'org.junit.platform.gradle.plugin'
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.0"
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.0"
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "http://dl.bintray.com/jetbrains/spek" }
}
}
junitPlatform {
filters {
engines {
include 'spek'
}
}
}
Run Code Online (Sandbox Code Playgroud)
模块 …
我正在尝试将Spek测试框架添加到我的Android Studio项目中.按照说明在这里,我最终添加以下内容到我的模块build.gradle
:
testCompile 'org.jetbrains.spek:spek-api:1.1.5'
testCompile 'junit:junit:4.12'
testCompile "org.junit.platform:junit-platform-runner:1.0.0"
testRuntimeOnly 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
Run Code Online (Sandbox Code Playgroud)
然后我注释了我的测试 @RunWith(JUnitPlatform::class)
但是,当我尝试运行测试时,我得到:
org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
知道我错过了什么吗?
我试图将我的测试区分为单元测试和集成测试。我的想法是使用新的JUnit5批注@Tag("unit")
,该批注对我的JUnit测试非常有效,但我无法使其与Spek一起使用。
我现在的课程是:
data class MyObject(val value: Int)
Run Code Online (Sandbox Code Playgroud)
我的测试:
@Tag("unit")
object MyObjectTest {
@Test
fun checkEquality() {
val o1 = MyObject(1)
assertEquals(o1, o1)
}
}
Run Code Online (Sandbox Code Playgroud)
与我的build.gradle具有:
task utest(type: Test) {
outputs.upToDateWhen { false }
useJUnitPlatform {
includeEngines 'junit-jupiter', 'junit-vintage', 'spek'
includeTags 'unit'
excludeTags 'performance', 'integration', 'functional'
}
testLogging {
events "passed", "skipped", "failed"
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行utest时,这可行。但是,使用Spek进行相同操作时:
@Tag("unit")
object MyObjectSpek : Spek({
given("an Object") {
val o1 = MyObject(1)
it("should be equal to itself") {
assertEquals(o1, o1)
}
}
}) …
Run Code Online (Sandbox Code Playgroud) 我想要一个通用的测试装置:
@RunWith(JUnitPlatform::class)
abstract class BaseSpek: Spek({
beforeGroup {println("before")}
afterGroup {println("after")}
})
Run Code Online (Sandbox Code Playgroud)
现在我想使用该规范:
class MySpek: BaseSpek({
it("should xxx") {}
})
Run Code Online (Sandbox Code Playgroud)
但由于无参数BaseSpek
构造函数,我遇到了编译错误。实现我需要的正确方法是什么?
我在spek
试图找到测试时遇到了崩溃.我尝试过很多不同的版本和样本配置.我从命令行运行.Gradle 4.0,mac osx.任何帮助,将不胜感激!
这是错误:
WARNING: TestEngine with ID 'spek' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.findAllClassesInClasspathRoot(Ljava/nio/file/Path;Ljava/util/function/Predicate;Ljava/util/function/Predicate;)Ljava/util/List;
at org.jetbrains.spek.engine.SpekTestEngine.resolveSpecs(SpekTestEngine.kt:66)
at org.jetbrains.spek.engine.SpekTestEngine.discover(SpekTestEngine.kt:50)
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.junit.platform.launcher.Launcher.execute(Launcher.java:96)
at org.junit.platform.console.tasks.ConsoleTestExecutor.executeTests(ConsoleTestExecutor.java:65)
at org.junit.platform.console.tasks.ConsoleTestExecutor.lambda$execute$0(ConsoleTestExecutor.java:57)
at org.junit.platform.console.tasks.CustomContextClassLoaderExecutor.invoke(CustomContextClassLoaderExecutor.java:33)
at org.junit.platform.console.tasks.ConsoleTestExecutor.execute(ConsoleTestExecutor.java:57)
at org.junit.platform.console.ConsoleLauncher.executeTests(ConsoleLauncher.java:85)
at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:75)
at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:48)
at org.junit.platform.console.ConsoleLauncher.main(ConsoleLauncher.java:40)
Run Code Online (Sandbox Code Playgroud)
这是我当前的build.gradle:
buildscript {
repositories {
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$project.ext.kotlinVersion"
}
}
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'idea'
apply plugin: 'kotlin'
apply plugin: 'java'
junitPlatform {
filters …
Run Code Online (Sandbox Code Playgroud)