Android Studio 3.0 Canary 1:引用 Kotlin 类的 Kotlin 测试或 Java 测试失败

2022-09-01 05:26:42

更新

此问题已在此处提交错误: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”Empty test suite.

因此,我用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
}

我的模块特定 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"
}

答案 1

解决方法(目前):

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

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

然后在底部的“启动前”修改测试JUnit运行/调试配置,其中有“Gradle感知make”,另一个部分,选择,选择它所在的项目文件,然后键入。单击此处查看不同测试框架的屏幕截图,但管道的工作方式相同。+gradle taskbuild.gradlecopyTestClasses

您可能需要更改/添加更多目录管道,具体取决于您的构建类型。找到这些奇怪地方的方法是在项目树中野蛮搜索相关的.class文件。


答案 2

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

感谢@kat向我展示了正确的方向。首先 - 已经为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配置。然后,我单击了Android JUnit>默认值,并按如下方式编辑了配置:

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

这就是它最终的样子:enter image description here


推荐