我正在尝试使用Robolectric(1.2)和Dagger测试应用程序。Robolectric和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和匕首,也许我做错事的实施,我不认为在配置中。 我应该检查什么以确保一切正常?你们看到这段代码有什么明显的错误吗?
Hey Kap,我正在尝试类似的事情,并且在构建我的测试模块图时收到了令人困惑的异常。你介意看看是否看到类似的东西吗? http://pastebin.com/G33aHkAa - 干杯。 – OceanLife 2013-06-10 00:01:56
从我的踪迹可以看出,这是一个不同的问题。你的问题看起来更像是一个模块配置问题。匕首的getModuleAdapter吐出一些注释问题... – 2013-06-27 10:30:14
谢谢Kap:我在SO上发布了这个问题,并在找到解决方案时自我回答。更多细节[here](http://stackoverflow.com/questions/17144824/robolectric-dagger-and-compile-time-moduleadapter-creation)。 – OceanLife 2013-06-27 13:02:24