I just configured a test project for my Android app to use Robolectric. I followed the Eclipse Quick Start. An exception is raised executing my simple very first test.
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)
[...]
The test follows.
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);
}
}
It seems it can't find Android ./res
folder. I pointed the JUnit configuration to ${workspace_loc:teldir-android}
as the guide says. And this makes Eclipse to find the AndroidManifest.xml
, avoiding other errors, imho.
Trying to avoid this exception I also added ./res
folder as source folder of my Android app, cleaned and relaunched all, but the same exception raised.
What am I doing wrong?
I had to add an environment variable to my test run configuration:
ANDROID_HOME=C:/path/to/android-sdks
I had the exact same issue (RuntimeException when loading the resources) when first setting up my Eclipse project with Robolectric but now it works.
I figured two things out:
WARNING: Unable to find path to Android SDK
(the message might not be displayed in a version lower than 1.0-RC1) then ResourceLoader
failed to find your SDK root. One of the methods looks for an ANDROID_HOME
environment variable that should point to where your Android SDK resides.Using Robolectric without Maven or Ant is possible but there might be a few more bugs. This video shows the correct project setup. If it doesn't work, get the latest version from Github (import as a Maven project in Eclipse and you might need github.com/mosabua/maven-android-sdk-deployer) and start debugging!
If the ResourceNotFound only occurs for string arrays defined with <array>...</array>
tags, replace them with <string-array>...</string-array>
Using robolectric 2.0
read in: https://groups.google.com/forum/#!topic/robolectric/gQoY0bCguYs
docs: http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With