我正在尝试使用 Robolectric (1.2) 和 Dagger 测试应用程序。
我的测试模块很简单:
@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);
...
包含的 ServiceModule 包括 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;
}
一切都很好,我可以在 Eclipse 中调试,直到XmlPullParserFactory.newInstance()被命中。那时,我收到了可怕的消息:
java.lang.RuntimeException: Stub!
at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java:13)
at ....
at ....
我对 Robolectric 和 Dagger 比较陌生,也许我在实施中做错了什么,我不认为在配置中。我应该检查什么以确保一切正常?你们在这段代码中看到任何明显的错误吗?