首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试环境配置: Android + JUnit 5+ Mockito + Spek + Kotlin

测试环境配置: Android + JUnit 5+ Mockito + Spek + Kotlin
EN

Stack Overflow用户
提问于 2017-09-12 15:05:13
回答 1查看 3.4K关注 0票数 6

我很难配置基于JUnit木星(5)的测试环境。我有两个不同的错误:

代码语言:javascript
复制
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)...

配置如下。

build.gradle

代码语言:javascript
复制
    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'
        }
    }
}

模块build.gradle

代码语言:javascript
复制
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'de.mannodermaus.android-junit5'

android {

    compileSdkVersion 26
    buildToolsVersion "26.0.0"

    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    ...

    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
}


dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.0.2'
    implementation 'com.android.support:recyclerview-v7:26.0.2'

    testImplementation 'org.jetbrains.spek:spek-api:1.1.4'
    testImplementation 'org.jetbrains.spek:spek-junit-platform-engine:1.1.4'

    testImplementation junit5()
//    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.0.0' not needed when using the one above
    testImplementation 'org.junit.platform:junit-platform-runner:1.0.0'

    testImplementation 'org.mockito:mockito-core:2.8.47'
    testImplementation 'com.nhaarman:mockito-kotlin-kt1.1:1.5.0'
    testCompileOnly "de.mannodermaus.gradle.plugins:android-junit5-embedded-runtime:1.0.0"
}

这个配置应该基于https://github.com/aurae/android-junit5。但我也试过不戴它。

有人设法为这些库找到依赖项的工作配置吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-29 09:13:41

正如在这里发布的https://stackoverflow.com/a/48427771/4249825,您需要在您的build.gradle中使用以下内容:

app的build.grale:

代码语言:javascript
复制
buildscript {
    ...
    dependencies {
        ...
        classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.30"  // latest atm
    }
}

模块的build.gradle:

代码语言:javascript
复制
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "de.mannodermaus.android-junit5"

android {
    ...
    sourceSets {
        test.java.srcDirs += 'src/test/kotlin'
        androidTest.java.srcDirs += 'src/androidTest/kotlin'
    }
}

project.ext {
    spekVersion = "1.1.5"  // latest atm
    mockitoKotlinVersion = "2.0.0-alpha02"  // latest atm
    kluentVersion = "1.34"  // latest atm
}

dependencies {
    ...
    //
    // TESTS
    testCompile "com.nhaarman.mockitokotlin2:mockito-kotlin:$mockitoKotlinVersion"
    testImplementation "org.amshove.kluent:kluent-android:$kluentVersion"
    testImplementation("org.jetbrains.spek:spek-api:$spekVersion") {
        exclude group: "org.jetbrains.kotlin"
    }
    testImplementation("org.jetbrains.spek:spek-junit-platform-engine:$spekVersion") {
        exclude group: "org.junit.platform"
        exclude group: "org.jetbrains.kotlin"
    }
    testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

    testImplementation junit5.unitTests()
    // see https://github.com/mannodermaus/android-junit5#android-studio-workarounds
    testCompileOnly junit5.unitTestsRuntime()
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46179837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档