我刚刚为我的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应用程序的源文件夹,清理并重新启动了所有内容,但是仍然出现了相同的异常。我做错了什么?