diff --git a/daggermock-kotlin/src/main/java/it/cosenonjaviste/daggermock/DaggerMock.kt b/daggermock-kotlin/src/main/java/it/cosenonjaviste/daggermock/DaggerMock.kt index 17a0266..fe07a8e 100644 --- a/daggermock-kotlin/src/main/java/it/cosenonjaviste/daggermock/DaggerMock.kt +++ b/daggermock-kotlin/src/main/java/it/cosenonjaviste/daggermock/DaggerMock.kt @@ -37,5 +37,11 @@ class DaggerMock(val rule: DaggerMockRule) { DaggerMock(rule).init() return rule } + + inline fun ruleWithSpys(spyAllUnMockedDependencies: Boolean = false, vararg modules: Any, noinline init: DaggerMock.() -> Unit = {}): DaggerMockRule { + val rule = DaggerMockRule(spyAllUnMockedDependencies, C::class.java, *modules) + DaggerMock(rule).init() + return rule + } } } \ No newline at end of file diff --git a/daggermock/src/main/java/it/cosenonjaviste/daggermock/DaggerMockRule.java b/daggermock/src/main/java/it/cosenonjaviste/daggermock/DaggerMockRule.java index 138b334..f3a3442 100644 --- a/daggermock/src/main/java/it/cosenonjaviste/daggermock/DaggerMockRule.java +++ b/daggermock/src/main/java/it/cosenonjaviste/daggermock/DaggerMockRule.java @@ -37,12 +37,18 @@ public class DaggerMockRule implements MethodRule { private ComponentClassWrapper componentClass; private ComponentSetter componentSetter; private BuilderCustomizer customizer; + private boolean spyUnMockedDependencies; private List modules = new ArrayList<>(); private final List dependencies = new ArrayList<>(); private final Map, ObjectWrapper> dependenciesWrappers = new HashMap<>(); private final Map, ComponentSetter> dependentComponentsSetters = new HashMap<>(); private final OverriddenObjectsMap overriddenObjectsMap = new OverriddenObjectsMap(); + public DaggerMockRule(boolean spyUnMockedDependencies, Class componentClass, Object... modules) { + this(componentClass, modules); + this.spyUnMockedDependencies = spyUnMockedDependencies; + } + public DaggerMockRule(Class componentClass, Object... modules) { this.componentClass = new ComponentClassWrapper<>(componentClass); Collections.addAll(this.modules, modules); @@ -123,7 +129,7 @@ private void setupComponent(Object target) { overriddenObjectsMap.init(target); overriddenObjectsMap.checkOverriddenInjectAnnotatedClass(modules); - ModuleOverrider moduleOverrider = new ModuleOverrider(overriddenObjectsMap); + ModuleOverrider moduleOverrider = new ModuleOverrider(overriddenObjectsMap, spyUnMockedDependencies); overriddenObjectsMap.checkOverridesInSubComponentsWithNoParameters(componentClass); diff --git a/daggermock/src/main/java/it/cosenonjaviste/daggermock/ModuleOverrider.java b/daggermock/src/main/java/it/cosenonjaviste/daggermock/ModuleOverrider.java index 1785377..a4acb94 100644 --- a/daggermock/src/main/java/it/cosenonjaviste/daggermock/ModuleOverrider.java +++ b/daggermock/src/main/java/it/cosenonjaviste/daggermock/ModuleOverrider.java @@ -42,13 +42,16 @@ public class ModuleOverrider { private final OverriddenObjectsMap overriddenObjectsMap; + private boolean spyUnMockedDependencies = false; + public ModuleOverrider(Object target) { - this(new OverriddenObjectsMap()); + this(new OverriddenObjectsMap(), false); overriddenObjectsMap.init(target); } - public ModuleOverrider(OverriddenObjectsMap overriddenObjectsMap) { + public ModuleOverrider(OverriddenObjectsMap overriddenObjectsMap, boolean spyUnMockedDependencies) { this.overriddenObjectsMap = overriddenObjectsMap; + this.spyUnMockedDependencies = spyUnMockedDependencies; } public T override(final T module) { @@ -56,14 +59,21 @@ public T override(final T module) { Answer defaultAnswer = new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { + Object obj = null; Method method = invocation.getMethod(); Provider provider = overriddenObjectsMap.getProvider(method); if (provider != null) { - return provider.get(); + obj = provider.get(); } else { method.setAccessible(true); - return method.invoke(module, invocation.getArguments()); + Object originalObj = method.invoke(module, invocation.getArguments()); + if (spyUnMockedDependencies) { + obj = Mockito.spy(originalObj); + } else { + obj = originalObj; + } } + return obj; } }; return (T) Mockito.mock(module.getClass(), defaultAnswer);