Robolectric + Eclipse无法找到资源?

时间:2011-03-17 14:15:20

标签: android eclipse junit4 robolectric

我刚刚为我的Android应用配置了测试项目以使用Robolectric。 我跟着Eclipse Quick Start。 执行我的简单第一次测试时会出现异常。

java.lang.RuntimeException: java.lang.NullPointerException
    at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:93)
    at com.xtremelabs.robolectric.res.ResourceLoader.getStringValue(ResourceLoader.java:271)
    at com.xtremelabs.robolectric.shadows.ShadowResources.getString(ShadowResources.java:56)
    at android.content.res.Resources.getString(Resources.java)
    at org.xxx.mobile.android.teldir.app.TelephoneDirectoryTest.searchButtonLabelShouldBeGo(TelephoneDirectoryTest.java:22)
    [...]
Caused by: java.lang.NullPointerException
    at com.xtremelabs.robolectric.res.StringResourceLoader.getValue(StringResourceLoader.java:17)
    at com.xtremelabs.robolectric.res.StringArrayResourceLoader.processNode(StringArrayResourceLoader.java:39)
    at com.xtremelabs.robolectric.res.XpathResourceXmlLoader.processResourceXml(XpathResourceXmlLoader.java:27)
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlFile(DocumentLoader.java:58)
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlDir(DocumentLoader.java:52)
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlDir(DocumentLoader.java:39)
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResourcesFromDir(ResourceLoader.java:142)
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResourcesFromDirs(ResourceLoader.java:136)
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResources(ResourceLoader.java:109)
    at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:85)
    at com.xtremelabs.robolectric.res.ResourceLoader.getStringValue(ResourceLoader.java:271)
    at com.xtremelabs.robolectric.shadows.ShadowResources.getString(ShadowResources.java:56)
    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 com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:87)
    at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:110)
    at android.content.res.Resources.getString(Resources.java)
    [...]

测试如下。

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.xtremelabs.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class TelephoneDirectoryTest {
    private TelephoneDirectory activity;

    @Before
    public void setUp() {
        activity = new TelephoneDirectory();
    }

    @Test
    public void searchButtonLabelShouldBeGo() throws Exception {
        String goLabel = activity.getResources().getString(R.string.search);
        assertEquals("Go", goLabel);

    }
}

似乎无法找到Android ./res文件夹。正如指南所说,我将JUnit配置指向${workspace_loc:teldir-android}。这使得Eclipse找到了AndroidManifest.xml,避免了其他错误,imho。

为了避免这种异常,我还添加了./res文件夹作为我的Android应用程序的源文件夹,清理并重新启动了所有文件夹,但引发了相同的异常。

我做错了什么?

6 个答案:

答案 0 :(得分:6)

我必须在我的测试运行配置中添加一个环境变量:

ANDROID_HOME=C:/path/to/android-sdks

答案 1 :(得分:4)

首次使用Robolectric设置我的Eclipse项目时,我遇到了完全相同的问题(加载资源时出现RuntimeException),但现在它可以正常工作。

我想出了两件事:

  • 如果您收到WARNING: Unable to find path to Android SDK(该消息可能不会显示在低于1.0-RC1的版本中),则ResourceLoader无法找到您的SDK根目录。其中一种方法是查找应该指向Android SDK所在位置的ANDROID_HOME环境变量。
  • 最后,如果您的menu.xml没有以某种方式格式化,则会有bug导致异常。

使用没有Maven或Ant的Robolectric是可能的,但可能还有一些错误。 This视频显示了正确的项目设置。如果它不起作用,从Github获取最新版本(在Eclipse中导入为Maven项目,您可能需要 github.com/mosabua/maven-android-sdk-deployer )并开始调试!

答案 2 :(得分:1)

如果仅对使用<array>...</array>标记定义的字符串数组发生ResourceNotFound,请将其替换为<string-array>...</string-array>

使用robolectric 2.0

请阅读:https://groups.google.com/forum/#!topic/robolectric/gQoY0bCguYs

docs:http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

答案 3 :(得分:0)

显然是错误的..我不得不设置一种蚂蚁方式来做事,因为你无法从eclipse启动robolectric测试,因为Eclipse工作区仍然无法正确识别链接源个别文件。请参阅我的alpha示例:

https://github.com/shareme/Wabash

我从道路实验室的方向得到的相同错误..

答案 4 :(得分:0)

刚刚安装完Windows 8后出现此问题。很可能Robolectric无法找到SDK。为了把它指向正确的地方。您可以在Android项目中生成local.properties文件。这可以在命令行中完成,方法是转到项目的根文件夹并发出命令:

android update project –p c:\project\root\folder

打开local.properties以验证sdk.dir是否指向预期路径。

还要确保已下载Android API级别8. Robolectric在/path/to/your/sdk/platforms/android-8目录中查找文件夹。

答案 5 :(得分:0)

你可以在这个链接中获取Jar文件。只需下载它。在项目中添加JAR 多数民众赞成我在我的项目中使用过。有任何疑问问我

http://www.java2s.com/Code/JarDownload/robolectric/robolectric-1.2-8.jar.zip