Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2941字,纯文字阅读大概需要5分钟。
内容图文
![Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败](/upload/InfoBanner/zyjiaocheng/708/02f9424eecd24aa3b3d472bbb2a6b149.jpg)
UPDATE
此处已针对此问题提交了一个错误:
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类“Wallet”无法找到:
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"
}
解决方法:
解决方法(暂时):
把它放到你的(app-level)build.gradle中:
task copyTestClasses(type: Copy) {
from "build/tmp/kotlin-classes/debugUnitTest"
into "build/intermediates/classes/debug"
}
然后在“启动前”的底部向下修改测试JUnit Run / Debug配置,其中包含“Gradle-aware make”,另一部分,选择gradle任务,选择它所在的项目build.gradle文件,然后键入copyTestClasses. Click here for screenshots的不同测试框架,但管道工作方式相同.
您可能需要根据构建类型更改/添加更多目录管道.您找到这些奇怪位置的方法是通过在项目树中搜索相关的.class文件.
内容总结
以上是互联网集市为您收集整理的Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败全部内容,希望文章能够帮你解决Android Studio 3.0 Canary 1:Kotlin测试或Java测试引用Kotlin类失败所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。