android - java.lang.ClassNotFoundException : android. R 错误

标签 android testing junit robolectric

我正在尝试使用 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"));
    }

}

有人可以帮助我理解为什么会出现此错误吗?

谢谢。

最佳答案

android.jar 库添加到类路径。

关于android - java.lang.ClassNotFoundException : android. R 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16950104/

相关文章:

javascript - 在 Meteor 中使用 Jasmine 测试异步函数

java - EasyMock 当我们在测试类上调用 db 时

java - 为 "Step"作用域 bean 编写 JUnit 测试 - 没有为作用域名称 "step"注册作用域(Spring Batch 3.0.10)

android - 短信类型常量

java - ImageView 在其可绘制对象之间闪烁

angular - 从 Docker 运行 Angular 测试脚本

java - Selenium 和 JUnit 在多个驱动程序中并行执行测试方法

java - Android 构建错误 com.google.firebase :firebase-core:16. 0.8

android - 如果我使用颜色作为背景,EditText 尺寸会改变吗?

unit-testing - 测试服务器端/客户端 coffeescript 代码