Kotlin:单元测试期间@JvmStatic方法NoClassDefFoundError / ClassNotFoundException
这是我试图从我的单元测试调用的类
package bj.discogsbrowser.artistreleases class ArtistResultFactory { companion object { @JvmStatic fun buildArtistResult(members: Int): ArtistResult { val artistResult = ArtistResult() artistResult.nameVariations = listOf("ye") artistResult.dataQuality = "really good quality, we have the best qualities" return artistResult } } }
这是测试的样子:
@Test fun setArtistNoMembers_displaysNoMembers() { controller.setArtist(ArtistResultFactory.buildArtistResult(0)) ... assertEquals(copyOfModels.size, 8) }
Java中的相同测试(也失败了):
@Test public void setArtistNoMembers_displaysNoMembers() { controller.setArtist(ArtistResultFactory.buildArtistResult(0)); ... assertEquals(copyOfModels.size(), 8); }
在Kotlin和Java中,我都得到堆栈跟踪:
java.lang.NoClassDefFoundError: bj/discogsbrowser/artistreleases/ArtistResultFactory at bj.discogsbrowser.artist.ArtistControllerTest.setArtistNoMembers_displaysNoMembers(ArtistControllerTest.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 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.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:488) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:209) at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109) at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36) 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.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63) 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: bj.discogsbrowser.artistreleases.ArtistResultFactory at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:161) at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:108) at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:101) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 32 more
我觉得这可能与我的sourceSets
因为当我删除这些行,那么代码将无法编译。
sourceSets { test { java.srcDirs += "$projectDir/src/testShared" } androidTest { java.srcDirs += "$projectDir/src/testShared" } }
ArtistResultFactory
位于/src/testShared/
。 当它是一个Java类时,测试运行良好。
我的应用程序build.gradle
包括
apply plugin: "kotlin-android" apply plugin: 'kotlin-android-extensions'
在顶部。
你需要使它成为一个“对象”而不是“class”才能在Kotlin中访问它的单例风格。
如果你确实把它作为一个对象(这是正确的事情,像一个工厂,通常是单身人士),那么在Java中,我相信你必须做一个.getINSTANCE()之前,你可以访问该方法(不知道如果你可以使它静态或不),因为我不相信在Kotlin的对象允许伴侣对象(我可能会误以为)。