使用 Maven Surefire 插件运行 Robolectric 测试时出现 ClassNotFoundException

发布于 2024-11-30 04:04:34 字数 6744 浏览 0 评论 0原文

我已经使用 Android Maven 集成在 Eclipse 中设置了一个项目,并使用 Robolectric 进行了一些测试。

当我将它们部署在 Eclipse 中时,测试运行良好。但是,当我尝试使用Maven“安装”目标(这实际上是我本地 Maven 存储库中需要的库)构建项目时,它在这些相同的测试中失败了。

日志显示以下错误:

testAll((package).MyTest)  Time elapsed: 0.006 sec  <<< ERROR!
java.lang.RuntimeException: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for (package).R
    at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:316)
    at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:270)
    at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:221)
    at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:201)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:169)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
Caused by: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for (package).R
    at javassist.Loader.findClass(Loader.java:359)
    at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.findClass(RobolectricClassLoader.java:60)
    at javassist.Loader.loadClass(Loader.java:311)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:37)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:312)
    ... 22 more
Caused by: com.xtremelabs.robolectric.bytecode.IgnorableClassNotFoundException: msg because of javassist.NotFoundException: (package).R
    at com.xtremelabs.robolectric.bytecode.AndroidTranslator.onLoad(AndroidTranslator.java:80)
    at javassist.Loader.findClass(Loader.java:340)
    ... 29 more

其中 (package) 当然是我的主包的名称。这是我的 POM 的相关部分:

  <dependencies>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
      <version>2.1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>0.9.8</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
   <defaultGoal>package</defaultGoal>
    <plugins>
      <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>maven-android-plugin</artifactId>
        <version>2.8.3</version>
        <configuration>
        <genDirectory>${project.basedir}/gen</genDirectory>
          <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
          <assetsDirectory>${project.basedir}/assets</assetsDirectory>
          <resourceDirectory>${project.basedir}/res</resourceDirectory>
          <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
          <sdk>
            <platform>7</platform>
          </sdk>
          <deleteConflictingFiles>true</deleteConflictingFiles>
          <undeployBeforeDeploy>true</undeployBeforeDeploy>
        </configuration>
        <extensions>true</extensions>
      </plugin>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <excludes>
                <exclude>**/Test*.java</exclude>
            </excludes>
        </configuration>
    </plugin>      
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.8</version>
    </plugin>
    </plugins>
  </build>

如您所见,我尝试将“genDirectory”选项添加到 Maven Android 插件中。但无济于事。

为了让 Surefire 插件“看到”R 类,我缺少什么?

另一件事:我的代码实际上并未引用 R 类。

I've set up a project in Eclipse using the Android Maven integration, and Robolectric for some tests.

The tests run fine when I deploy them in Eclipse. However, when I try to build the project with the Maven "install" goal (this is actually a library that I need in my local Maven repository), it fails on these same tests.

Logs show the following error:

testAll((package).MyTest)  Time elapsed: 0.006 sec  <<< ERROR!
java.lang.RuntimeException: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for (package).R
    at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:316)
    at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:270)
    at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:221)
    at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:201)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:169)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
Caused by: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for (package).R
    at javassist.Loader.findClass(Loader.java:359)
    at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.findClass(RobolectricClassLoader.java:60)
    at javassist.Loader.loadClass(Loader.java:311)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:37)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:312)
    ... 22 more
Caused by: com.xtremelabs.robolectric.bytecode.IgnorableClassNotFoundException: msg because of javassist.NotFoundException: (package).R
    at com.xtremelabs.robolectric.bytecode.AndroidTranslator.onLoad(AndroidTranslator.java:80)
    at javassist.Loader.findClass(Loader.java:340)
    ... 29 more

where (package) is of course the name of my main package. Here's the relevant section of my POM:

  <dependencies>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
      <version>2.1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>0.9.8</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
   <defaultGoal>package</defaultGoal>
    <plugins>
      <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>maven-android-plugin</artifactId>
        <version>2.8.3</version>
        <configuration>
        <genDirectory>${project.basedir}/gen</genDirectory>
          <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
          <assetsDirectory>${project.basedir}/assets</assetsDirectory>
          <resourceDirectory>${project.basedir}/res</resourceDirectory>
          <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
          <sdk>
            <platform>7</platform>
          </sdk>
          <deleteConflictingFiles>true</deleteConflictingFiles>
          <undeployBeforeDeploy>true</undeployBeforeDeploy>
        </configuration>
        <extensions>true</extensions>
      </plugin>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <excludes>
                <exclude>**/Test*.java</exclude>
            </excludes>
        </configuration>
    </plugin>      
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.8</version>
    </plugin>
    </plugins>
  </build>

As you can see, I've tried adding the "genDirectory" option to the Maven Android plugin. But to no avail.

What am I missing to make the surefire plugin "see" the R class?

One more thing: the R class is not actually referenced by my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

゛清羽墨安 2024-12-07 04:04:34

我也正好遇到这个问题。

所以我仔细比较了 RobolectricSample 项目和我的项目。

令我惊讶的是,如果我不先运行“mvn clean test”命令行,RobolectricSample 项目也无法工作。

因此我分析了差异,发现命令行也将“gen/xxx/R.java”编译为“target/classes/xxx/R”。

这就是成功的秘诀,所以我执行了以下步骤:
1.右键项目-> Java 构建;
2.将“{project}/gen”输出文件夹更改为“target/class”(而不是原来的“target/android-classes”)

就是这样,希望这会有所帮助。

I also got exactly this issue.

So I compared carefully between RobolectricSample project and my project.

To my surprise, the RobolectricSample project also could NOT work if I didn't run the command line of "mvn clean test" first.

Therefore I analysed the difference and found that the command line compiled the "gen/xxx/R.java" to "target/classes/xxx/R" too.

That's the trick of success so I did the following steps:
1. right click project -> Java Build;
2. change the "{project}/gen" output folder as "target/class" (instead of original "target/android-classes")

That's it and hope this helps.

卖梦商人 2024-12-07 04:04:34

我发现解决此问题的唯一方法是将假 R.java 文件放入包中:

    public class R {
        public static final class attr {
        }

        public static final class drawable {
        }

        public static final class id {
        }

        public static final class layout {
        }

        public static final class string {
        }
    }

仅在以下情况下才需要这样做:

  1. 的 Maven 库项目
  2. 单元测试依赖项
  3. 未生成或未找到 Robolectric R.java 在 Maven 类路径上。

注意:您可以将 R.java 放入测试源中,这样就不会部署它。这不会干扰创建的实际资源。

I found that the only way to fix this issue was to place a fake R.java file into the package:

    public class R {
        public static final class attr {
        }

        public static final class drawable {
        }

        public static final class id {
        }

        public static final class layout {
        }

        public static final class string {
        }
    }

This should only be necessary under the following circumstances:

  1. Maven library project
  2. unit test dependencies on Robolectric
  3. R.java not being generated or not found on maven classpath.

Note: You can put R.java in your test sources so that it isn't deployed. This won't then interfere with actual resources created.

长不大的小祸害 2024-12-07 04:04:34

添加 Robolectric 后第一次尝试从 Eclipse 运行测试时,我遇到了同样的问题。

为我解决问题的是命令行上的 mvn clean test
之后,在 Eclipse 中测试也运行正常。

并确保您的 pom.xml 中有 apk

I've had the same problem when trying to run a test from Eclipse, first time after adding Robolectric.

What solved the problem for me was mvn clean test on the command line.
After that, the test ran OK from Eclipse as well.

And make sure you have <packaging>apk</packaging> in your pom.xml.

对你的占有欲 2024-12-07 04:04:34

在 eclipse 中和通过 mvn clean test 在命令行上运行 Robolectric 测试时,我遇到了同样的问题。即 java.lang.ClassNotFoundException 抱怨找不到我的 R.class 文件。事实上,没有 R.class 文件,因为我的项目是一个没有资源的 Android 库。

消除该错误的一个肮脏的技巧是使用单个未使用的字符串创建一个 /res/values/strings.xml 文件。然后生成 R.java 和相应的 R.class 并且错误消失。

I had this same issue when running Robolectric tests both in eclipse and on the command line via mvn clean test. I.e. a java.lang.ClassNotFoundException complaining that my R.class file was not found. Indeed, there was no R.class file since my project is an Android library that has no resources.

A dirty hack to get rid of the error is to create a <project-home>/res/values/strings.xml file with a single unused string. Then the R.java and the corresponding R.class are generated and the error goes away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文