【问题标题】:java.lang.ClassNotFoundException: android.Rjava.lang.ClassNotFoundException: android.R
【发布时间】:2013-06-05 21:21:05
【问题描述】:

我正在尝试使用 Robolectric 2.2 运行简单的 junit 测试,但使用 ADT 包 22 时出现以下错误:

java.lang.NoClassDefFoundError: android/R
    at org.robolectric.bytecode.Setup.<clinit>(Setup.java:40)
    at org.robolectric.RobolectricTestRunner.createSetup(RobolectricTestRunner.java:137)
    at org.robolectric.RobolectricTestRunner.createSdkEnvironment(RobolectricTestRunner.java:115)
    at org.robolectric.RobolectricTestRunner$3.create(RobolectricTestRunner.java:278)
    at org.robolectric.EnvHolder.getSdkEnvironment(EnvHolder.java:21)
    at org.robolectric.RobolectricTestRunner.getEnvironment(RobolectricTestRunner.java:276)
    at org.robolectric.RobolectricTestRunner.access$100(RobolectricTestRunner.java:57)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:190)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: android.R
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 24 more

给出错误的代码如下:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import com.mycomp.myapp.R;

@RunWith(RobolectricTestRunner.class)
public class ListToWatchActivityTest {  

    @Test
    public void shouldHaveApplicationName() throws Exception {
        String appName = new ListToWatchActivity().getResources().getString(R.string.app_name);
        assertThat(appName, equalTo("MyApp"));
    }

}

谁能帮我理解为什么会出现这个错误?

谢谢。

【问题讨论】:

  • 你真的应该这样创建一个新的活动实例吗?另外,我认为您并不想在应用程序包名称和“MyApp”之间进行比较,因为它们总是不同的......
  • 可能是由于缺少导入按 ctrl+shft+o ,然后清理并运行您的代码
  • 你是如何运行junit的?马文?蚂蚁?
  • @sheidaei:在libs 文件夹中添加android.jar 让一切正常。我无法理解为什么构建路径中的源文件夹test 无法找到也在构建路径中的android.jar
  • @skip 我在不同的模块中遇到过类似的问题。他们应该调查构建路径。我不确定为什么会发生这种情况,这可能是因为一些重叠的规范覆盖了 eclipse 中第三方模块的构建路径。这是我最好的猜测。这就是为什么如果我遇到这类问题,我总是在本地测试它。很高兴您发现并解决了问题。

标签: android testing junit robolectric


【解决方案1】:

android.jar 库添加到类路径。

【讨论】:

  • 这对我有用。但是我不得不将 项移到 j-unit-4.11 下面的 .iml 文件中。
猜你喜欢
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多