Top Description Constructors Methods
org.easymock.internal

public Class Injector

extends Object
Class Inheritance
Imports
org.easymock.*, java.lang.reflect.Constructor, .Field, .Modifier, java.util.ArrayList, .List

Performs creation of mocks and injection into test subjects in accordance with annotations present in the host object.
Authors
Henri Tremblay, Alistair Todd
Since
3.3

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
createMocksForAnnotations(Class<?>
class to search
hostClass
,
Object
object of the class
host
,
InjectionPlan
output parameter where the created mocks and fields to inject are added
injectionPlan
)

Create the mocks and find the fields annotated with TestSubject

private static List<Field>
fieldsOf(Class<?> clazz)

private static Injection
private static Field
getFieldByName(Class<?> clazz, String fieldName)

private static List<Field>
injectByName(Class<?> clazz, Object obj, List<Injection> qualifiedInjections)

private static void
injectByType(Object obj, List<Field> fields, List<Injection> injections)

private static InjectionTarget
public static void
injectMocks(Object
the object on which to inject mocks
host
)

Inject a mock to every fields annotated with Mock on the class passed in parameter.

private static void
injectMocksOnClass(Class<?>
class where the fields are taken
clazz
,
Object
object being a instance of clazz
obj
,
InjectionPlan
details of possible mocks for injection
injectionPlan
)

Try to inject a mock to every fields in the class

pack-priv static <T> T
private static MockType
private static boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Injectorback to summary
public Injector()

Method Detail

createMocksForAnnotationsback to summary
private static void createMocksForAnnotations(Class<?> hostClass, Object host, InjectionPlan injectionPlan)

Create the mocks and find the fields annotated with TestSubject

Parameters
hostClass:Class<?>

class to search

host:Object

object of the class

injectionPlan:InjectionPlan

output parameter where the created mocks and fields to inject are added

fieldsOfback to summary
private static List<Field> fieldsOf(Class<?> clazz)
findUniqueAssignableback to summary
private static Injection findUniqueAssignable(List<Injection> injections, InjectionTarget target)
getFieldByNameback to summary
private static Field getFieldByName(Class<?> clazz, String fieldName)
injectByNameback to summary
private static List<Field> injectByName(Class<?> clazz, Object obj, List<Injection> qualifiedInjections)
injectByTypeback to summary
private static void injectByType(Object obj, List<Field> fields, List<Injection> injections)
injectionTargetWithFieldback to summary
private static InjectionTarget injectionTargetWithField(Field f)
injectMocksback to summary
public static void injectMocks(Object host)

Inject a mock to every fields annotated with Mock on the class passed in parameter. Then, inject these mocks to the fields of every class annotated with TestSubject.

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
Then, ignoring all fields and mocks matched by field name
  • 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
Fields are searched recursively on the superclasses

Note

If the parameter extends EasyMockSupport, the mocks will be created using it to allow replayAll/verifyAll to work afterwards

Parameters
host:Object

the object on which to inject mocks

Since
3.2
injectMocksOnClassback to summary
private static void injectMocksOnClass(Class<?> clazz, Object obj, InjectionPlan injectionPlan)

Try to inject a mock to every fields in the class

Parameters
clazz:Class<?>

class where the fields are taken

obj:Object

object being a instance of clazz

injectionPlan:InjectionPlan

details of possible mocks for injection

instantiateTestSubjectback to summary
pack-priv static <T> T instantiateTestSubject(Field f)
mockTypeFromAnnotationback to summary
private static MockType mockTypeFromAnnotation(Mock annotation)
shouldNotAssignToback to summary
private static boolean shouldNotAssignTo(Field f)