Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to provide Spy dependencies #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ class DaggerMock<C>(val rule: DaggerMockRule<C>) {
DaggerMock<C>(rule).init()
return rule
}

inline fun <reified C> ruleWithSpys(spyAllUnMockedDependencies: Boolean = false, vararg modules: Any, noinline init: DaggerMock<C>.() -> Unit = {}): DaggerMockRule<C> {
val rule = DaggerMockRule(spyAllUnMockedDependencies, C::class.java, *modules)
DaggerMock<C>(rule).init()
return rule
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ public class DaggerMockRule<C> implements MethodRule {
private ComponentClassWrapper<C> componentClass;
private ComponentSetter<C> componentSetter;
private BuilderCustomizer customizer;
private boolean spyUnMockedDependencies;
private List<Object> modules = new ArrayList<>();
private final List<DependentComponentInfo> dependencies = new ArrayList<>();
private final Map<Class<?>, ObjectWrapper<?>> dependenciesWrappers = new HashMap<>();
private final Map<Class<?>, ComponentSetter<?>> dependentComponentsSetters = new HashMap<>();
private final OverriddenObjectsMap overriddenObjectsMap = new OverriddenObjectsMap();

public DaggerMockRule(boolean spyUnMockedDependencies, Class<C> componentClass, Object... modules) {
this(componentClass, modules);
this.spyUnMockedDependencies = spyUnMockedDependencies;
}

public DaggerMockRule(Class<C> componentClass, Object... modules) {
this.componentClass = new ComponentClassWrapper<>(componentClass);
Collections.addAll(this.modules, modules);
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,38 @@ 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> T override(final T module) {
checkMethodsVisibility(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);
Expand Down