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

Introduce mockAutowired() in ReflectionTestUtils #1613

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions spring-test/spring-test.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
optional("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
optional("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
optional("io.projectreactor:reactor-test")
optional("org.mockito:mockito-core:1.10.19")
testCompile(project(":spring-context-support"))
testCompile(project(":spring-oxm"))
testCompile("javax.cache:cache-api:1.0.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -452,4 +456,34 @@ private static String safeToString(@Nullable Object target) {
}
}

public static Map<Class<?>, Object> mockAutowired(Object target) throws IllegalAccessException {
Map<Class<?>, Object> beans = new HashMap<>();
for(Field field : target.getClass().getDeclaredFields()) {
if (field.getDeclaredAnnotation(Autowired.class) == null) {
continue;
}

Object value = Mockito.mock(field.getType());
beans.put(field.getType(), value);
field.setAccessible(true);
field.set(target, value);
}

return beans;
}

@SuppressWarnings("unchecked")
public static <T> T getBean(Map<Class<?>, Object> beans, Class<T> type) {
Object v = beans.get(type);
if (v == null) {
return null;
}

Class<?> dataType = v.getClass();
if (dataType.equals(type) || type.isAssignableFrom(v.getClass())) {
return (T) v;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.util.subpackage.Component;
import org.springframework.test.util.subpackage.LegacyEntity;
import org.springframework.test.util.subpackage.Person;
import org.springframework.test.util.subpackage.PersonEntity;
import org.springframework.test.util.subpackage.StaticFields;

import java.util.Map;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
Expand Down Expand Up @@ -421,5 +424,28 @@ public void invokeSetterMethodOnLegacyEntityWithSideEffectsInToString() {
invokeSetterMethod(entity, "collaborator", testCollaborator);
assertTrue(entity.toString().contains(testCollaborator));
}
@Test
public void invokeMockAutowiredWillAutoMockAllAutowiredFields() throws IllegalAccessException {
MockAutowiredStub stub = new MockAutowiredStub();
Map<Class<?>, Object> beans = mockAutowired(stub);
assertNotNull(beans);

assertNotNull(getBean(beans, LegacyEntity.class));
assertNotNull(getBean(beans, Component.class));
}
private static class MockAutowiredStub {
@Autowired
private LegacyEntity legacyEntity;

@Autowired
private Component component;

public LegacyEntity getLegacyEntity() {
return legacyEntity;
}

public Component getComponent() {
return component;
}
}
}