【问题标题】:Android Studio 3.0 Canary 1: Kotlin tests or Java tests referring to Kotlin classes failAndroid Studio 3.0 Canary 1:引用 Kotlin 类的 Kotlin 测试或 Java 测试失败
【发布时间】:2017-10-20 03:59:50
【问题描述】:

更新

已在此处针对此问题提交了一个错误: https://youtrack.jetbrains.com/issue/KT-17951

更新 2

该错误已在 Android Studio 3.0 Canary 3 中修复

原帖

我刚开始使用 Android Studio 3.0,我从一开始就启用了 kotlin 支持。我在我的项目中写了一个非常简单的 Kotlin 类:

data class Wallet(val coins: Int) {
    fun add(value: Int): Wallet = Wallet(coins + value)
    fun substract(value: Int): Wallet = if (coins > value) Wallet(coins + value) else throw InsufficientFundsException()
}

现在我想测试这个类,首先我在Kotlin中写了一个本地运行的unittest(测试目录):

class WalletTestKotlin {

    @Throws(Exception::class)
    @Test
    fun add() {
        Assert.assertEquals(22, Wallet(20).add(2).coins.toLong())
        Assert.assertNotEquals(5, Wallet(2).add(13).coins.toLong())
    }
}

它编译并运行,但出现错误消息:

找不到类: "com.agentknopf.hachi.repository.model.WalletTestKotlin"空测试 套房。

因此我用 Java 重新编写了测试:

public class WalletTest {

    @Throws(exceptionClasses = Exception.class)
    @Test
    public void add() {
        Assert.assertEquals(22, new Wallet(20).add(2).getCoins());
        Assert.assertNotEquals(5, new Wallet(2).add(13).getCoins());
    }
}

但是该测试也失败了 - 这次找不到 Kotlin 类“钱包”:

java.lang.NoClassDefFoundError: com/example/repository/model/Wallet

我想知道我是否遗漏了什么...运行 Java 测试,它不引用 Kotlin 类,但仅成功完成 Java 类。

我的项目build.gradle文件是默认的:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的 Module-specific build.gradle 的依赖关系:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //Kotlin support
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    //Testing libraries
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}

【问题讨论】:

  • 这是一个bug,很快就会修复youtrack.jetbrains.com/issue/KT-17951
  • 尝试在 Android Studio 3.0 Canary 4 上运行 Espresso 测试时找不到 Java 类。这是一个已知问题吗?
  • 我在 beta 2 中遇到了同样的问题

标签: java android android-studio junit kotlin


【解决方案1】:

此错误已在 Android Studio 3.0 Canary 3 中修复

【讨论】:

  • 正确 - 在验证它已修复后,我在此线程上的答案中添加了注释
  • @fangmobile.com 这可能是一种回归 - 在这里尝试任何答案 - 他们现在可能会(再次)解决问题。
【解决方案2】:

注意:问题已在 Android Studio 3.3 Canary 中得到修复

感谢@kat 向我展示了正确的方向。首先 - bug had been filed 用于 OP 中提到的问题。

我的设置如下: Kotlin 测试与 Java 测试位于同一目录中。要让这两个用例都能正常工作:

  • 参考 Java 测试中的 kotlin 类
  • 在 kotlin 测试中参考 kotlin 类

首先删除您可能拥有的任何其他测试运行配置。然后我在我的应用级 build.gradle 中添加了这两个 gradle 构建任务:

android {
    ...

    task copyTestClasses(type: Copy) {
        from "build/tmp/kotlin-classes/debugUnitTest"
        into "build/intermediates/classes/debug"
    }

    task copySdkClasses(type: Copy) {
        from "build/tmp/kotlin-classes/debug"
        into "build/intermediates/classes/debug"
    }
}

然后我通过运行 > 编辑配置打开了运行配置菜单。在左侧,我删除了顶级 Android JUnit 配置。 然后我点击 Defaults > Android JUnit 并编辑配置如下:

  • 测试种类 = 全部包装
  • 表单模式 = 无
  • 重复 = 一次
  • Package = 我的基本包,例如 com.yourname
  • 搜索测试 = 在单个模块中
  • 我没有改动中间部分(VM 选项等)
  • 在“发布前”部分,通过单击加号图标添加两个条目:
  • 运行 gradle 任务:选择 app 模块的 build.gradle,然后在任务中输入名称 copyTestClasses 并单击确定
  • 像以前一样添加第二个 Run gradle 任务,但这次作为任务名称输入 copySdkClasses 并单击 OK
  • 点击配置上的应用,现在运行您的测试

最后是这个样子的:

【讨论】:

  • Android Studio 3 Canary 3 不再需要
  • @Entreco 太棒了,感谢您的提醒!我会仔细检查以确保并在我的答案中添加注释,不再需要以下内容。
  • 谢谢!终于解决了这个问题。我在多个版本的 AS 上都有这个,还有 Normal 和最新的 Canary。但奇怪的是,由于我在第一个项目中解决了这个问题,它自动解决了其他项目。
【解决方案3】:

@AgentKnopf 刚刚好,但是你需要像这样在每个新的测试用例中添加任务

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile("com.android.support.test.espresso:espresso-core:$espresso_version", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile "junit:junit:$junit_version"
    // kotlin
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    ...............
}
// add 2 task in run config to make kotlin test working
// for junit
task copyTestClasses(type: Copy) {
    from "build/tmp/kotlin-classes/debugUnitTest"
    into "build/intermediates/classes/debug"
}
// for instrumented test
task copySdkClasses(type: Copy) {
    from "build/tmp/kotlin-classes/debug"
    into "build/intermediates/classes/debug"
}

afterEvaluate {
    compileDebugUnitTestSources.dependsOn copyTestClasses
    compileReleaseUnitTestSources.dependsOn copyTestClasses

    compileDebugAndroidTestSources.dependsOn copySdkClasses
}

自动为您添加任务

【讨论】:

  • 我认为我的建议应该适用于所有测试用例,如果你将它添加到默认配置中(只需确保删除以前的测试运行配置)。但是,如果您的解决方案在没有向测试配置添加任何内容的情况下确实有效,那就更好了:)
  • 问题似乎在 Android Studio 3 Canary 3 中得到解决
【解决方案4】:

解决方法(目前):

将其放入您的(应用级)build.gradle:

task copyTestClasses(type: Copy) {
    from "build/tmp/kotlin-classes/debugUnitTest"
    into "build/intermediates/classes/debug"
}

然后修改底部'before launch'下的test JUnit Run/Debug配置,里面有'Gradle-aware make',+另一个部分,选择gradle task,选择项目build.gradle文件就是并输入copyTestClassesClick here for screenshots 不同的测试框架,但管道的工作方式相同。

您可能需要根据构建类型更改/添加更多目录管道。找到那些奇怪的地方的方法是在项目树中搜索相关的 .class 文件。

【讨论】:

  • 我做了上述 - 但是现在我在调用 kotlin 类的 kotlin 测试和调用 kotlin 类的 java 测试中得到 NoClassDefFoundError 。我在 java 目录中有我的 kotlin 测试(我也尝试将它们放在 kotlin 测试目录中,但结果相同)。我看到的与您发布的链接的不同之处在于,在运行配置中我没有选择 Junit - 我只有 Android JUnit,所以我为那个和我的 Kotlin 测试进行了配置 - 不过运气不好。
  • 我让它工作了:) - 我还需要一些东西。我会接受您的回答并发布更多详细信息 - 谢谢!
  • 问题似乎在 Android Studio 3 Canary 3 中得到解决
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 2017-10-17
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多