2017-05-20 7 views
1

My Kotlinクラスは問題なくコンパイルされ、エミュレータで実行されます。しかし、私はそれをテストすると、Androidスタジオはクラスが見つからないと言います。NoClassDefFoundError実行時のJUnitテストのKotlinクラス

ここで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 

ここでは、モジュールレベルbuild.gradledependenciesセクション来る:

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

は私が間違って何をしたのですか?

+0

[Androidスタジオ3.0カナリー1:KotlinテストまたはKotlinクラスを参照するJavaテストが失敗する可能性があります](http://stackoverflow.com/questions/44079918/android-studio-3-0-canary-1-kotlin test-or-java-tests-refer-to-kotlin-clas) – jsc0

答えて

1

これは現在、最新のツールでは機能しません。このためにJetbrainsトラッカーにはopen issueがあります。

1

解決策は、モジュールbuild.gradleファイルで新しいタスクを作成することです。

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

次に、テスト実行構成の[起動前]セクションにそのタスクを追加します。

enter image description here

+0

クラスとテストクラスの2つのタスクを作成しなければなりませんでした。 – Sah

1

これは、私はそれがすぐに修正される未解決の問題を推測さ:

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

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

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

一方、あなたは次の回避策を使用することができます

あなたのGradleファイルに追加します(後にアンドロイド{...}節)これらのタスクを実行しますステップ「打ち上げの前に」その後

、あなたのテストの設定を開き、新しい追加:

enter image description here

enter image description here

enter image description here

+0

ありがとう、これは私のために働いた –

関連する問題