Robolectric+Eclipse 无法找到资源?

9

我刚刚为我的Android应用程序配置了一个测试项目,使用Robolectric。 我遵循了Eclipse快速入门的步骤。 在执行我的第一个简单测试时,会引发异常。

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,避免其他错误。为了避免这个异常,我还将./res文件夹添加为我的Android应用程序的源文件夹,清理并重新启动了所有内容,但是仍然出现了相同的异常。我做错了什么?

你也可以尝试使用Maven。这是示例代码:https://github.com/pivotal/RobolectricSample - Macarse
你能把你的导入代码粘贴上来吗? - Sunil Pandey
@Sunil,我刚刚粘贴了我的导入内容。 - Giorgio Vespucci
你在这方面有进展吗?我也遇到了完全相同的错误。 - ahsteele
@ahsteele 对此没有好消息,甚至来自Robolectric Google Group也没有。 - Giorgio Vespucci
6个回答

6

我需要在测试运行配置中添加一个环境变量:

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

这是我在Intellij中成功的做法。我已经设置好了相应的环境变量...我想我只需要在构建配置中添加引用就可以了。谢谢! - Kent Andersen

4

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

我找到了两个解决方法:

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

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


1

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 目录中查找一个文件夹。


0

显然是错的..我不得不设置一种方法来完成任务,因为无法从Eclipse中启动Robolectric测试,因为Eclipse工作区仍然不能正确识别链接源文件。请参考我的示例:

https://github.com/shareme/Wabash

顺便说一下,我得到了与Pivotal Labs指南相同的错误。


你是在说如果不像 Wabash 那样(还没看到),那么我唯一的方法就是像 RoboletricSample 一样,在同一个 Android 项目中直接放置一个./test 文件夹来进行测试吗?如果是这样,我想我可以做到... - Giorgio Vespucci

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接