更新
已在此处针对此问题提交了一个错误: 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())
}
}
它编译并运行,但出现错误消息:
Class not found: "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
}
我的 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"
}
最佳答案
解决方法(目前):
将其放入您的(应用级)build.gradle:
task copyTestClasses(type: Copy) {
from "build/tmp/kotlin-classes/debugUnitTest"
into "build/intermediates/classes/debug"
}
然后在底部'before launch'中修改测试JUnit Run/Debug配置,里面有'Gradle-aware make',+
另一部分,选择gradle task
,选择它所在的项目 build.gradle
文件,然后键入 copyTestClasses
。 Click here for screenshots不同的测试框架,但管道的工作方式相同。
您可能需要根据构建类型更改/添加更多目录管道。找到那些奇怪的地方的方法是在项目树中搜索相关的 .class 文件。
关于java - Android Studio 3.0 金丝雀 1 : Kotlin tests or Java tests referring to Kotlin classes fail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44079918/