@Singletonpublic class MyAPI { @Inject public MyAPI(Context context) { //Do something } public MyObject getMyFeed(){ }}
这是我正在尝试设置的测试类:
@RunWith(RobolectricTestRunner.class)public class MyFragmentTest extends TestCase { @Inject MyAPIInterceptor myAPI; @Inject Activity shadowActivity; @Inject LayoutInflater shadowLayoutInflator; @Inject VIEwGroup shadowVIEwGroup; @Before public voID setUp() throws Exception { Robolectric.bindShadowClass(SingleThreadActivity.class); ShadowAPIModule module = new ShadowAPIModule(); Module roboGuiceModule = RoboGuice.newDefaultRoboModule(Robolectric.application); RoboGuice.setBaseApplicationInjector(Robolectric.application,RoboGuice.DEFAulT_STAGE,roboGuiceModule); RoboInjector injector = RoboGuice.getInjector(Robolectric.application); injector.injectMembers(this); } @After public voID tearDown() { RoboGuice.util.reset(); Application app = Robolectric.application; DefaultRoboModule defaultModule = RoboGuice.newDefaultRoboModule(app); RoboGuice.setBaseApplicationInjector(app,defaultModule); } @Test public voID testOnCreateVIEw() throws Exception{ myAPI.mock = mock(MyAPI.class); when(myAPI.mock.getMyFeed()).thenReturn(new MyObject()); MyFragment frag = new MyFragment(); frag.onAttach(shadowActivity); frag.onCreate(null); frag.onCreateVIEw(shadowLayoutInflator,shadowVIEwGroup,null); frag.onActivityCreated(null); frag.onStart(); frag.onResume(); }}@Singletonclass MyAPIInterceptor extends MyAPI{ public MyAPI mock; @Inject public MyAPIInterceptor(Context context) { super(context); } @OverrIDe public MyObject getMyFeed() throws Exception { return mock.getMyFeed(); }}@Implements(Activity.class)class SingleThreadActivity extends ShadowActivity{ @OverrIDe public voID runOnUiThread(Runnable action) { action.run(); }}class ShadowAPIModule extends AbstractModule { @OverrIDe protected voID configure() { bind(Context.class).to(MockContext.class); bind(VIEwGroup.class).toInstance(mock(VIEwGroup.class)); bind(MyAPI.class).to(MyAPIInterceptor.class); }}
但是,当我运行测试时,我得到以下内容:
com.Google.inject.ConfigurationException: Guice configuration errors:1) No implementation for androID.vIEw.VIEwGroup was bound. while locating androID.vIEw.VIEwGroup for fIEld at com.mysource.MyFragmentTest.shadowVIEwGroup(UnkNown Source) while locating com.mysource.MyFragmentTest1 error at com.Google.inject.internal.InjectorImpl.getMembersInjector(InjectorImpl.java:952) at com.Google.inject.internal.InjectorImpl.getMembersInjector(InjectorImpl.java:957) at com.Google.inject.internal.InjectorImpl.injectMembers(InjectorImpl.java:943) at roboguice.inject.ContextScopedRoboInjector.injectMembersWithoutVIEws(ContextScopedRoboInjector.java:243) at roboguice.inject.ContextScopedRoboInjector.injectMembers(ContextScopedRoboInjector.java:236) at com.mysource.MyFragmentTest.setUp(RSSFeedActivityTest.java:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at com.xtremelabs.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:292) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner.run(ParentRunner.java:193) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.accessModule roboGuiceModule = RoboGuice.newDefaultRoboModule(Robolectric.application);0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IDeaTestRunner.startRunnerWithArgs(JUnit4IDeaTestRunner.java:76) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
根据我对roboguice的理解,似乎由于某种原因它不能用ShadowVIEwGroup注入“shadowVIEwGroup”但我不确定为什么.如果我以错误的方式解决这个问题,那么请让我知道,但这似乎应该有效.
你能告诉我们:
1)为什么我的测试不起作用
要么
2)注入类使用的自定义单例的更好方法是什么?
代替:
Module roboGuiceModule = Modules.overrIDe(RoboGuice.newDefaultRoboModule(Robolectric.application)).with(new ShadowAPIModule())
试试这个:
bind(MyAPI.class).to(MyAPIImpl.class).in(Singleton.class);
评论的答案:
如果你在测试中遇到单例问题,那就把它删除吧.应该隔离测试,因此不需要单独模式.
在生产代码中,而不是@Singleton类注释,在Module中设置与Singleton.class的绑定
bind(MyAPI.class).to(MyAPIImpl.class);
对于测试模块,在您的ShadowModule案例中,设置不带Singleton的绑定.
注入阴影类的另一种情况.您应该注入必须获取注入成员或具有不同实现的对象.除非有自定义实现的绑定,否则无需注入VIEwGroup或Activity.
看看第一个Robolectric示例:http://pivotal.github.com/robolectric/只有构造函数,它的工作原理.
总结以上是内存溢出为你收集整理的android – 如何用robolectric覆盖注入的@Singleton类?全部内容,希望文章能够帮你解决android – 如何用robolectric覆盖注入的@Singleton类?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)