Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric+Eclipse Can't find resources?

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?

like image 615
Giorgio Vespucci Avatar asked Mar 17 '11 14:03

Giorgio Vespucci


3 Answers

I had to add an environment variable to my test run configuration:

ANDROID_HOME=C:/path/to/android-sdks
like image 178
koljaTM Avatar answered Sep 27 '22 17:09

koljaTM


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:

  • If you receive a 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.
  • Finally, there is a bug which causes an exception if your menu.xml is not formatted in a certain way.

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!

like image 37
mfellner Avatar answered Sep 27 '22 15:09

mfellner


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

like image 44
Maragues Avatar answered Sep 27 '22 16:09

Maragues