Package org.easymock.internal
Class Injector
- java.lang.Object
-
- org.easymock.internal.Injector
-
public class Injector extends java.lang.ObjectPerforms creation of mocks and injection into test subjects in accordance with annotations present in the host object.- Since:
- 3.3
-
-
Constructor Summary
Constructors Constructor Description Injector()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static voidcreateMocksForAnnotations(java.lang.Class<?> hostClass, java.lang.Object host, InjectionPlan injectionPlan)Create the mocks and find the fields annotated withTestSubjectprivate static java.util.List<java.lang.reflect.Field>fieldsOf(java.lang.Class<?> clazz)private static InjectionfindUniqueAssignable(java.util.List<Injection> injections, InjectionTarget target)private static java.lang.reflect.FieldgetFieldByName(java.lang.Class<?> clazz, java.lang.String fieldName)private static java.util.List<java.lang.reflect.Field>injectByName(java.lang.Class<?> clazz, java.lang.Object obj, java.util.List<Injection> qualifiedInjections)private static voidinjectByType(java.lang.Object obj, java.util.List<java.lang.reflect.Field> fields, java.util.List<Injection> injections)private static InjectionTargetinjectionTargetWithField(java.lang.reflect.Field f)static voidinjectMocks(java.lang.Object host)Inject a mock to every fields annotated withMockon the class passed in parameter.private static voidinjectMocksOnClass(java.lang.Class<?> clazz, java.lang.Object obj, InjectionPlan injectionPlan)Try to inject a mock to every fields in the class(package private) static <T> TinstantiateTestSubject(java.lang.reflect.Field f)private static MockTypemockTypeFromAnnotation(Mock annotation)private static booleanshouldNotAssignTo(java.lang.reflect.Field f)
-
-
-
Method Detail
-
injectMocks
public static void injectMocks(java.lang.Object host)
Inject a mock to every fields annotated withMockon the class passed in parameter. Then, inject these mocks to the fields of every class annotated withTestSubject.The rules are
- Static and final fields are ignored
- If two mocks have the same field name, return an error
- If a mock has a field name and no matching field is found, return an error
- If a mock without field name can be assigned to a field, do it. The same mock can be assigned more than once
- If no mock can be assigned to a field, skip the field silently
- If the mock cannot be assigned to any field, skip the mock silently
- If two mocks can be assigned to the same field, return an error
Note: If the parameter extends
EasyMockSupport, the mocks will be created using it to allowreplayAll/verifyAllto work afterwards- Parameters:
host- the object on which to inject mocks- Since:
- 3.2
-
instantiateTestSubject
static <T> T instantiateTestSubject(java.lang.reflect.Field f)
-
createMocksForAnnotations
private static void createMocksForAnnotations(java.lang.Class<?> hostClass, java.lang.Object host, InjectionPlan injectionPlan)Create the mocks and find the fields annotated withTestSubject- Parameters:
hostClass- class to searchhost- object of the classinjectionPlan- output parameter where the created mocks and fields to inject are added
-
injectMocksOnClass
private static void injectMocksOnClass(java.lang.Class<?> clazz, java.lang.Object obj, InjectionPlan injectionPlan)Try to inject a mock to every fields in the class- Parameters:
clazz- class where the fields are takenobj- object being a instance of clazzinjectionPlan- details of possible mocks for injection
-
injectByName
private static java.util.List<java.lang.reflect.Field> injectByName(java.lang.Class<?> clazz, java.lang.Object obj, java.util.List<Injection> qualifiedInjections)
-
injectByType
private static void injectByType(java.lang.Object obj, java.util.List<java.lang.reflect.Field> fields, java.util.List<Injection> injections)
-
fieldsOf
private static java.util.List<java.lang.reflect.Field> fieldsOf(java.lang.Class<?> clazz)
-
getFieldByName
private static java.lang.reflect.Field getFieldByName(java.lang.Class<?> clazz, java.lang.String fieldName)
-
injectionTargetWithField
private static InjectionTarget injectionTargetWithField(java.lang.reflect.Field f)
-
shouldNotAssignTo
private static boolean shouldNotAssignTo(java.lang.reflect.Field f)
-
findUniqueAssignable
private static Injection findUniqueAssignable(java.util.List<Injection> injections, InjectionTarget target)
-
-