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

6 浏览
0 Comments

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

更新

已经在此问题上提交了一个错误报告:

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中编写了一个本地运行的单元测试(测试目录):

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类"Wallet":

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

我想知道我是否漏掉了什么... 运行一个不引用Kotlin类而只引用Java类的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支持
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    //测试库
    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"
}

0