AndroidStudio 3.0金丝雀1:引用Kotlin类的Kotlin测试或Java测试失败

AndroidStudio 3.0金丝雀1:引用Kotlin类的Kotlin测试或Java测试失败,java,android,android-studio,junit,kotlin,Java,Android,Android Studio,Junit,Kotlin,更新 已针对此问题在此处提交了一个bug: 更新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:

更新

已针对此问题在此处提交了一个bug:

更新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类,只会成功完成

My project 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"
}
task copyTestClasses(type: Copy) {
    from "build/tmp/kotlin-classes/debugUnitTest"
    into "build/intermediates/classes/debug"
}
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"
    }
}
解决方法(目前):

将其放入您的(应用程序级别)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"
}
task copyTestClasses(type: Copy) {
    from "build/tmp/kotlin-classes/debugUnitTest"
    into "build/intermediates/classes/debug"
}
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"
    }
}
然后在下面的“启动前”中修改测试JUnit运行/调试配置,其中有“Gradle aware make”,另一部分,选择
Gradle任务
,选择它所在的项目
build.Gradle
文件,然后键入
copyTestClasses
。不同的测试框架,但管道的工作方式相同

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

注意:安卓Studio 3.3金丝雀解决了这个问题

感谢@kat为我指明了正确的方向。首先,对于OP中提到的问题,答案是a

我的设置如下:Kotlin测试与Java测试位于同一目录中。要使这两个用例都起作用,请执行以下操作:

  • 请参阅Java测试中的kotlin类
  • 参考kotlin测试中的kotlin类
首先删除您可能拥有的任何其他测试运行配置。然后,我在我的应用程序级别build.gradle中添加了这两个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"
}
task copyTestClasses(type: Copy) {
    from "build/tmp/kotlin-classes/debugUnitTest"
    into "build/intermediates/classes/debug"
}
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任务:选择应用程序模块的build.gradle,然后在任务中输入名称copyTestClasses并单击确定
  • 像以前一样添加第二个Run gradle任务,但这次作为任务名称输入copySdkClasses并单击OK
  • 单击配置上的应用,现在运行测试

这就是它最后的样子:

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


自动为您添加任务

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

这是一个错误,很快就会修复,在尝试在Android Studio 3.0 Canary 4上运行浓缩咖啡测试时找不到Java类。这是一个已知的问题吗?我在beta 2I中遇到了相同的问题,我做了上述操作-但是现在我在调用kotlin类的kotlin测试和调用kotlin类的java测试中遇到了NoClassDefFoundError。我在java目录中有我的kotlin测试(我也尝试在kotlin测试目录中进行测试,但结果相同)。我看到你发布的链接的不同之处在于,在运行配置中,我没有一个选项说Junit-我只有Android Junit,所以我为那一个和我的Kotlin测试做了配置-但是运气不好。我让它工作了:)-我还需要一些东西。我会接受你的答案,并发布一个更详细的补充答案-谢谢!这个问题似乎在Android Studio 3 Canary 3中得到了解决。我认为我的建议应该适用于所有测试用例,如果您将其添加到默认配置中(只需确保删除您测试的先前运行配置)。但是,如果您的解决方案在不向测试配置添加任何内容的情况下实际工作,那么这就更好了:)问题似乎在Android Studio 3 Canary 3中得到了解决,不再需要Android Studio 3 Canary3@Entreco太棒了,谢谢你的提醒!我会再次检查,以确保并在我的答案上添加注释,即不再需要以下内容。谢谢!终于解决了这个问题。我在AS的多个版本上都有这个,还有普通版和最新的金丝雀版。但奇怪的是,自从我在第一个项目中解决了这个问题,它就自动解决了其他项目。正确-我在验证了它是正确的之后,在这个线程的答案中添加了一个注释fixed@fangmobile.com这可能是一种倒退——尝试一下这里的任何答案——他们可能(再次)暂时解决这个问题。