java.lang.NoClassDefFoundError引起:java.lang.ClassNotFoundException

时间:2018-02-22 10:29:04

标签: android testing gradle

我已将我的项目拆分为两个模块:app和domain,清洁架构应该有更多,但在我想继续前进之前我遇到了一个问题。

我添加了一个新模块:domain:并在其中添加了一个test文件夹,并将我的测试从:app模块移到了此文件夹中。不幸的是,在测试文件时我遇到了错误

java.lang.NoClassDefFoundError: Lbel/mobile/domain/interactor/accounts/GetAccountList;

at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
at java.lang.Class.getDeclaredFields(Class.java:1916)
at org.junit.runners.model.TestClass.getSortedDeclaredFields(TestClass.java:77)
at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:70)
at org.junit.runners.model.TestClass.<init>(TestClass.java:57)
at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:88)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:83)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.mockito.internal.runners.DefaultInternalRunner$1.<init>(DefaultInternalRunner.java:27)
at org.mockito.internal.runners.DefaultInternalRunner.<init>(DefaultInternalRunner.java:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.mockito.internal.runners.util.RunnerProvider.newInstance(RunnerProvider.java:27)
at org.mockito.internal.runners.RunnerFactory.create(RunnerFactory.java:69)
at org.mockito.internal.runners.RunnerFactory.createStrict(RunnerFactory.java:40)
at org.mockito.junit.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:152)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
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)

Caused by: java.lang.ClassNotFoundException: bel.mobile.domain.interactor.accounts.GetAccountList
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)
... 33 more

这是我的模块域的build.gradle文件:

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

buildscript {
    ext.kotlin_version = '1.2.10'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
}
repositories {
    mavenCentral()
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "io.reactivex.rxjava2:rxjava:$project.ext.rxJavaVersion"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "junit:junit:$project.ext.junitVersion"
    implementation "org.mockito:mockito-core:$project.ext.mockitoVersion"
    implementation "com.google.truth:truth:$project.ext.truthVersion"
}

这是 domain.iml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":domain" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="android-gradle" name="Android-Gradle">
      <configuration>
        <option name="GRADLE_PROJECT_PATH" value=":domain" />
      </configuration>
    </facet>
    <facet type="java-gradle" name="Java-Gradle">
      <configuration>
        <option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
        <option name="BUILDABLE" value="true" />
      </configuration>
    </facet>
    <facet type="kotlin-language" name="Kotlin">
      <configuration version="3" platform="JVM 1.8" useProjectSettings="false">
        <compilerSettings />
        <compilerArguments>
          <option name="destination" value="$MODULE_DIR$/build/classes/kotlin/main" />
          <option name="noStdlib" value="true" />
          <option name="noReflect" value="true" />
          <option name="moduleName" value="domain" />
          <option name="jvmTarget" value="1.8" />
          <option name="addCompilerBuiltIns" value="true" />
          <option name="loadBuiltInsFromDependencies" value="true" />
          <option name="languageVersion" value="1.2" />
          <option name="apiVersion" value="1.2" />
          <option name="pluginOptions">
            <array />
          </option>
          <option name="pluginClasspaths">
            <array />
          </option>
        </compilerArguments>
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
    <output url="file://$MODULE_DIR$/build/classes/java/main" />
    <output-test url="file://$MODULE_DIR$/build/classes/java/test" />
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
      <excludeFolder url="file://$MODULE_DIR$/.gradle" />
      <excludeFolder url="file://$MODULE_DIR$/build" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.6" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="kotlin-stdlib-jdk8-1.2.10" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="truth-0.39" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="junit-4.12" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="mockito-core-2.8.9" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.1" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="kotlin-stdlib-jdk7-1.2.10" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="kotlin-stdlib-1.2.10" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="hamcrest-core-1.3" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="byte-buddy-1.6.14" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="byte-buddy-agent-1.6.14" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="objenesis-2.5" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="guava-23.4-android" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="error_prone_annotations-2.1.3" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="annotations-13.0" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="jsr305-1.3.9" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="j2objc-annotations-1.1" level="project" />
    <orderEntry type="library" exported="" scope="PROVIDED" name="animal-sniffer-annotations-1.14" level="project" />
  </component>
</module>

到目前为止我试图做的事情:

  • 我尝试了很多不同的方法并检查了很多关于同一错误的解决方案来解决问题,但没有一个能解决它
  • 我已经检查过该文件夹中的简单测试。
  • 我添加了Multidex

更新

同样在 build / reports /../ index.html 文件中,我的报告显示所有测试都是100%成功。这是在Android工作室右侧的Gradle选项卡中启动:domain 模块下的一些命令后完成的新测试。

1 个答案:

答案 0 :(得分:0)

你试过这个吗? NoClassDefFoundError while trying to run unit tests on Android Studio

特别是

  

2)在你的应用程序的build.gradle中你需要:

defaultConfig {
    applicationId "bel.mobile.domain"
    testApplicationId "bel.mobile.domain.test"
    ...
}