运行时JUnit测试中的Kotlin类上的NoClassDefFoundError

时间:2017-05-20 12:30:01

标签: android-studio kotlin

我的Kotlin类在没有问题的情况下编译并在模拟器上运行。但是当我测试它时,Android Studio说没有找到该类。

这是Kotlin类和使用它的一个例子:

// Product.kt
data class Product(
    val id: Int,
    val name: String,
    val manufacturer: String)

// MainActivity.java
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Product product = new Product(0, "foo", "bar");
        TextView textView = findViewById(R.id.textView);
        textView.setText(product.getName());
    }
}

enter image description here

但是当我为Kotlin类编写JUnit测试时:

// ProductTest.java
public class ProductTest {
    @Test
    public void getName() throws Exception {
        Product p = new Product(0, "foo", "bar");
        assertThat(p.getName(), equalTo("foo"));
    }
}

Android Studio编译代码,但拒绝运行测试:

java.lang.NoClassDefFoundError: com/example/Product

  at com.example.ProductTest.getName(ProductTest.java:15)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.lang.ClassNotFoundException: com.example.Product
  at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  ... 28 more

以下是模块级dependencies中的build.gradle部分:

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
    testCompile 'junit:junit:4.12'
}

我做错了什么?

4 个答案:

答案 0 :(得分:1)

目前这不适用于最新工具。在Jetbrains跟踪器中有一个open issue

答案 1 :(得分:1)

解决方案是在模块build.gradle文件中创建一个新任务。

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

然后将该任务添加到"发布之前"测试运行配置的一部分。

enter image description here

答案 2 :(得分:1)

这是一个悬而未决的问题,我想它很快就会解决:

https://youtrack.jetbrains.com/issue/KT-17951

https://issuetracker.google.com/issues/38454212

同时,您可以使用以下解决方法:

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"
}

将其添加到您的Gradle文件(在android {...}部分之后)

之后,打开测试配置并在启动之前添加新的""将执行这些任务的步骤:

enter image description here

enter image description here

enter image description here

答案 3 :(得分:0)

有人证实此错误已在Android Studio 3 Canary 3中得到修复:https://issuetracker.google.com/issues/38454212