我刚刚为我的 Android 应用程序配置了一个测试项目以使用 Robolectric。
我跟着Eclipse 快速入门 http://pivotal.github.com/robolectric/eclipse-quick-start.html。
执行我简单的第一个测试时出现异常。
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);
}
}
安卓好像找不到./res
文件夹。我将 JUnit 配置指向${workspace_loc:teldir-android}
正如导游所说。这使得 Eclipse 找到AndroidManifest.xml
,避免其他错误,恕我直言。
为了避免这个异常我还添加了./res
文件夹作为我的 Android 应用程序的源文件夹,清理并重新启动所有文件夹,但引发了相同的异常。
我究竟做错了什么?
我必须在测试运行配置中添加一个环境变量:
ANDROID_HOME=C:/path/to/android-sdks
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)