3

I am trying to test an app using Robolectric (1.2) and Dagger.

My testing module is very simple:

@RunWith(RobolectricTestRunner.class)
public class XmlTests {
    // Injected
    @Inject XmlPullParser mParser;
    @Inject AlarmActionSpawner mActionSpawner;
    @Inject FileHelper mFileHelper;
    @Inject XmlSerializer mSerializer;
    @Inject ConcurrentObjectMonitor mObjMonitor;

    @Module(
        includes = ServicesModule.class,
        entryPoints = XmlTests.class,
        overrides = true
    )
    static class TestModule {
        public TestModule() { }
    }

    @Before
    public void setUp() throws Exception {
        Log.i("setUp() entering.");

        ObjectGraph og = ObjectGraph.create(new TestModule());
        og.validate();
        og.inject(this);
        ...

The included ServiceModule includes XmlServiceModule:

@Module
public class XmlServiceModule {

    @Provides @Singleton XmlSerializer provideXmlSerializer() {
        return android.util.Xml.newSerializer();
    }

    @Provides @Singleton XmlPullParser provideXmlPullParser() {
        XmlPullParser pullParser = null;
        try {
            XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
            pullParser = xppf.newPullParser();
        } catch (XmlPullParserException e) {
            throw new RuntimeException(e.getMessage());
        }
        return pullParser;
    }

Everything is fine, I can debug within eclipse, until XmlPullParserFactory.newInstance() is hit. At that point, I receive the dreaded:

java.lang.RuntimeException: Stub!
    at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java:13)
    at ....
    at ....

I am relatively new to Robolectric and Dagger, maybe I am doing something wrong in the implementation, I don't think in the configuration. What should I check to be sure that everything is fine? Do you guys see any obvious mistake in this code?

Andrea Richiardi
  • 703
  • 6
  • 21
  • Hey Kap, I am attempting something similar at the moment and I have received a puzzling exception whilst building my test module graph. Would you mind taking a look to see if you saw anything similar? http://pastebin.com/G33aHkAa - Cheers. – BrantApps Jun 10 '13 at 00:01
  • From what I can understand from your trace, it is a different issue. Your issue looks a little bit more like a module configuration problem. Dagger's getModuleAdapter spits out some annotation problem... – Andrea Richiardi Jun 27 '13 at 10:30
  • Thanks Kap: I posted the question on SO and self-answered when I found the solution. More details [here](http://stackoverflow.com/questions/17144824/robolectric-dagger-and-compile-time-moduleadapter-creation). – BrantApps Jun 27 '13 at 13:02

2 Answers2

1

As far as I can tell, XmlPullParser & its factory aren't supported by Robolectric.

You may want to add some new shadow classes. Learn how here.

alex
  • 6,359
  • 1
  • 23
  • 21
  • Oh...I might have a look and add it then, if they really need it...because 2.0 is out and I think they are going to use "real jars" (this is what I read...).. – Andrea Richiardi Mar 15 '13 at 04:00
1

Ok, while waiting for an answer on GitHub to see if it is worth building a shadow of XmlPullParser and XmlSerializer for Robolectric, I solved my problem this way. This is exactly the reason why I so like Dagger and Dependency Injector frameworks in general (and Maven, of course). The change has taken literally 10 minutes, less than writing this answer, and it's now working like a charm.

I added Kxml dependeny to my pom:

<dependency>
    <groupId>net.sf.kxml</groupId>
    <artifactId>kxml2</artifactId>
    <version>2.3.0</version>
    <scope>test</scope>
</dependency>

Then I changed my test module to:

@Module(
    includes = ServicesModule.class,
    entryPoints = XmlTests.class,
    overrides = true
)
static class TestModule {
    public TestModule() { }

    @Provides @Singleton XmlSerializer provideXmlSerializer() {
        return new KXmlSerializer();
    }

    @Provides @Singleton XmlPullParser provideXmlPullParser() {
        return new KXmlParser();
    }
}

Notice the override = true because the two methods are going to override the one in XmlServiceModule, creating a new branch of the object graph.

Andrea Richiardi
  • 703
  • 6
  • 21