DaggerMock icon indicating copy to clipboard operation
DaggerMock copied to clipboard

Mini hack for component builder support

Open ohadnav opened this issue 8 years ago • 2 comments
trafficstars

Since you can't mock dagger components (they are final), here is a little hack:

               if (moduleName.startsWith("Dagger")) {
                    moduleName = moduleName.substring(6);
                    for (Class<?> interfaceClass : moduleClass.getInterfaces()) {
                        if (Objects.equals(interfaceClass.getSimpleName(), moduleName)) {
                            moduleClass = interfaceClass;
                        }
                    }
                }

Within getSetterMethod under ObjectWrapper

And final tweak to DaggerMockRule in initComponent

private ObjectWrapper<Object> initComponent(ComponentClassWrapper<?> componentClass,
      List<Object> modules, ModuleOverrider moduleOverrider) {
    Class<Object> daggerComponent = componentClass.getDaggerComponentClass();
    ObjectWrapper<Object> builderWrapper =
        ObjectWrapper.invokeStaticMethod(daggerComponent, "builder");
    for (Object module : modules) {
      builderWrapper = builderWrapper.invokeBuilderSetter(module.getClass(),
          (module.getClass().getModifiers() & FINAL) == FINAL ? module
              : moduleOverrider.override(module));
    }
    return builderWrapper;
  }

Happy testing :-)

ohadnav avatar Jul 16 '17 20:07 ohadnav

Could you please provide an example of a test that fails without this modification? Thanks for your suggestion, I'll try to integrate it soon!

fabioCollini avatar Jul 20 '17 20:07 fabioCollini

Whenever a module is dependent on a non-mockable class (i.e. another component).

On Thu, Jul 20, 2017 at 11:46 PM Fabio Collini [email protected] wrote:

Could you please provide an example of a test that fails without this modification? Thanks for your suggestion, I'll try to integrate it soon!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fabioCollini/DaggerMock/issues/48#issuecomment-316824846, or mute the thread https://github.com/notifications/unsubscribe-auth/AK-E01j3Y-9FZ9a35jKgLOd8G2JiHS-rks5sP7ydgaJpZM4OZZ_k .

ohadnav avatar Jul 21 '17 20:07 ohadnav