public abstract class ReflectionTestUtils extends Object
ReflectionTestUtils is a collection of reflection-based utility
 methods for use in unit and integration testing scenarios.
 There are often times when it would be beneficial to be able to set a
 non-public field, invoke a non-public setter method, or
 invoke a non-public configuration or lifecycle
 callback method when testing code involving, for example:
 
private or protected field access as opposed to
 public setter methods for properties in a domain entity.@Autowired,
 @Inject, and
 @Resource which provides dependency
 injection for private or protected fields, setter methods,
 and configuration methods.@PostConstruct
 and @PreDestroy for lifecycle callback
 methods.In addition, several methods in this class provide support for static
 fields and static methods — for example,
 setField(Class, String, Object), getField(Class, String),
 invokeMethod(Class, String, Object...),
 invokeMethod(Object, Class, String, Object...), etc.
ReflectionUtils, 
AopTestUtils| Constructor and Description | 
|---|
| ReflectionTestUtils() | 
| Modifier and Type | Method and Description | 
|---|---|
| static Object | getField(Class<?> targetClass,
        String name) | 
| static Object | getField(Object targetObject,
        Class<?> targetClass,
        String name) | 
| static Object | getField(Object targetObject,
        String name) | 
| static Object | invokeGetterMethod(Object target,
                  String name)Invoke the getter method with the given  nameon the supplied
 target object with the suppliedvalue. | 
| static <T> T | invokeMethod(Class<?> targetClass,
            String name,
            Object... args)Invoke the static method with the given  nameon the supplied target
 class with the supplied arguments. | 
| static <T> T | invokeMethod(Object targetObject,
            Class<?> targetClass,
            String name,
            Object... args)Invoke the method with the given  nameon the providedtargetObject/targetClasswith the supplied arguments. | 
| static <T> T | invokeMethod(Object target,
            String name,
            Object... args)Invoke the method with the given  nameon the supplied target
 object with the supplied arguments. | 
| static void | invokeSetterMethod(Object target,
                  String name,
                  Object value)Invoke the setter method with the given  nameon the supplied
 target object with the suppliedvalue. | 
| static void | invokeSetterMethod(Object target,
                  String name,
                  Object value,
                  Class<?> type)Invoke the setter method with the given  nameon the supplied
 target object with the suppliedvalue. | 
| static void | setField(Class<?> targetClass,
        String name,
        Object value) | 
| static void | setField(Class<?> targetClass,
        String name,
        Object value,
        Class<?> type) | 
| static void | setField(Object targetObject,
        Class<?> targetClass,
        String name,
        Object value,
        Class<?> type)Set the field with the given  name/typeon the providedtargetObject/targetClassto the suppliedvalue. | 
| static void | setField(Object targetObject,
        String name,
        Object value) | 
| static void | setField(Object targetObject,
        String name,
        Object value,
        Class<?> type) | 
public static void setField(Object targetObject, String name, @Nullable Object value)
name on the
 provided targetObject to the supplied value.
 This method delegates to setField(Object, String, Object, Class),
 supplying null for the type argument.
targetObject - the target object on which to set the field; never nullname - the name of the field to set; never nullvalue - the value to setpublic static void setField(Object targetObject, @Nullable String name, @Nullable Object value, @Nullable Class<?> type)
name/type
 on the provided targetObject to the supplied value.
 This method delegates to setField(Object, Class, String, Object, Class),
 supplying null for the targetClass argument.
targetObject - the target object on which to set the field; never nullname - the name of the field to set; may be null if
 type is specifiedvalue - the value to settype - the type of the field to set; may be null if
 name is specifiedpublic static void setField(Class<?> targetClass, String name, @Nullable Object value)
name on
 the provided targetClass to the supplied value.
 This method delegates to setField(Object, Class, String, Object, Class),
 supplying null for the targetObject and type arguments.
 
This method does not support setting static final fields.
targetClass - the target class on which to set the static field;
 never nullname - the name of the field to set; never nullvalue - the value to setpublic static void setField(Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type)
name/type on the provided targetClass to
 the supplied value.
 This method delegates to setField(Object, Class, String, Object, Class),
 supplying null for the targetObject argument.
 
This method does not support setting static final fields.
targetClass - the target class on which to set the static field;
 never nullname - the name of the field to set; may be null if
 type is specifiedvalue - the value to settype - the type of the field to set; may be null if
 name is specifiedpublic static void setField(@Nullable Object targetObject, @Nullable Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type)
name/type
 on the provided targetObject/targetClass to the supplied
 value.
 If the supplied targetObject is a proxy, it will
 be unwrapped allowing
 the field to be set on the ultimate target of the proxy.
 
This method traverses the class hierarchy in search of the desired
 field. In addition, an attempt will be made to make non-public
 fields accessible, thus allowing one to set protected,
 private, and package-private fields.
 
This method does not support setting static final fields.
targetObject - the target object on which to set the field; may be
 null if the field is statictargetClass - the target class on which to set the field; may
 be null if the field is an instance fieldname - the name of the field to set; may be null if
 type is specifiedvalue - the value to settype - the type of the field to set; may be null if
 name is specifiedReflectionUtils.findField(Class, String, Class), 
ReflectionUtils.makeAccessible(Field), 
ReflectionUtils.setField(Field, Object, Object), 
AopTestUtils.getUltimateTargetObject(Object)@Nullable public static Object getField(Object targetObject, String name)
name
 from the provided targetObject.
 This method delegates to getField(Object, Class, String),
 supplying null for the targetClass argument.
targetObject - the target object from which to get the field;
 never nullname - the name of the field to get; never nullgetField(Class, String)@Nullable public static Object getField(Class<?> targetClass, String name)
name from the provided targetClass.
 This method delegates to getField(Object, Class, String),
 supplying null for the targetObject argument.
targetClass - the target class from which to get the static field;
 never nullname - the name of the field to get; never nullgetField(Object, String)@Nullable public static Object getField(@Nullable Object targetObject, @Nullable Class<?> targetClass, String name)
name
 from the provided targetObject/targetClass.
 If the supplied targetObject is a proxy, it will
 be unwrapped allowing
 the field to be retrieved from the ultimate target of the proxy.
 
This method traverses the class hierarchy in search of the desired
 field. In addition, an attempt will be made to make non-public
 fields accessible, thus allowing one to get protected,
 private, and package-private fields.
targetObject - the target object from which to get the field; may be
 null if the field is statictargetClass - the target class from which to get the field; may
 be null if the field is an instance fieldname - the name of the field to get; never nullgetField(Object, String), 
getField(Class, String), 
ReflectionUtils.findField(Class, String, Class), 
ReflectionUtils.makeAccessible(Field), 
ReflectionUtils.getField(Field, Object), 
AopTestUtils.getUltimateTargetObject(Object)public static void invokeSetterMethod(Object target, String name, Object value)
name on the supplied
 target object with the supplied value.
 This method traverses the class hierarchy in search of the desired
 method. In addition, an attempt will be made to make non-public
 methods accessible, thus allowing one to invoke protected,
 private, and package-private setter methods.
 
In addition, this method supports JavaBean-style property
 names. For example, if you wish to set the name property on the
 target object, you may pass either "name" or
 "setName" as the method name.
target - the target object on which to invoke the specified setter
 methodname - the name of the setter method to invoke or the corresponding
 property namevalue - the value to provide to the setter methodReflectionUtils.findMethod(Class, String, Class[]), 
ReflectionUtils.makeAccessible(Method), 
ReflectionUtils.invokeMethod(Method, Object, Object[])public static void invokeSetterMethod(Object target, String name, @Nullable Object value, @Nullable Class<?> type)
name on the supplied
 target object with the supplied value.
 This method traverses the class hierarchy in search of the desired
 method. In addition, an attempt will be made to make non-public
 methods accessible, thus allowing one to invoke protected,
 private, and package-private setter methods.
 
In addition, this method supports JavaBean-style property
 names. For example, if you wish to set the name property on the
 target object, you may pass either "name" or
 "setName" as the method name.
target - the target object on which to invoke the specified setter
 methodname - the name of the setter method to invoke or the corresponding
 property namevalue - the value to provide to the setter methodtype - the formal parameter type declared by the setter methodReflectionUtils.findMethod(Class, String, Class[]), 
ReflectionUtils.makeAccessible(Method), 
ReflectionUtils.invokeMethod(Method, Object, Object[])@Nullable public static Object invokeGetterMethod(Object target, String name)
name on the supplied
 target object with the supplied value.
 This method traverses the class hierarchy in search of the desired
 method. In addition, an attempt will be made to make non-public
 methods accessible, thus allowing one to invoke protected,
 private, and package-private getter methods.
 
In addition, this method supports JavaBean-style property
 names. For example, if you wish to get the name property on the
 target object, you may pass either "name" or
 "getName" as the method name.
target - the target object on which to invoke the specified getter
 methodname - the name of the getter method to invoke or the corresponding
 property nameReflectionUtils.findMethod(Class, String, Class[]), 
ReflectionUtils.makeAccessible(Method), 
ReflectionUtils.invokeMethod(Method, Object, Object[])@Nullable public static <T> T invokeMethod(Object target, String name, Object... args)
name on the supplied target
 object with the supplied arguments.
 This method delegates to invokeMethod(Object, Class, String, Object...),
 supplying null for the targetClass argument.
target - the target object on which to invoke the specified methodname - the name of the method to invokeargs - the arguments to provide to the methodinvokeMethod(Class, String, Object...), 
invokeMethod(Object, Class, String, Object...), 
MethodInvoker, 
ReflectionUtils.makeAccessible(Method), 
ReflectionUtils.invokeMethod(Method, Object, Object[]), 
ReflectionUtils.handleReflectionException(Exception)@Nullable public static <T> T invokeMethod(Class<?> targetClass, String name, Object... args)
name on the supplied target
 class with the supplied arguments.
 This method delegates to invokeMethod(Object, Class, String, Object...),
 supplying null for the targetObject argument.
targetClass - the target class on which to invoke the specified methodname - the name of the method to invokeargs - the arguments to provide to the methodinvokeMethod(Object, String, Object...), 
invokeMethod(Object, Class, String, Object...), 
MethodInvoker, 
ReflectionUtils.makeAccessible(Method), 
ReflectionUtils.invokeMethod(Method, Object, Object[]), 
ReflectionUtils.handleReflectionException(Exception)@Nullable public static <T> T invokeMethod(@Nullable Object targetObject, @Nullable Class<?> targetClass, String name, Object... args)
name on the provided
 targetObject/targetClass with the supplied arguments.
 This method traverses the class hierarchy in search of the desired
 method. In addition, an attempt will be made to make non-public
 methods accessible, thus allowing one to invoke protected,
 private, and package-private methods.
targetObject - the target object on which to invoke the method; may
 be null if the method is statictargetClass - the target class on which to invoke the method; may
 be null if the method is an instance methodname - the name of the method to invokeargs - the arguments to provide to the methodinvokeMethod(Object, String, Object...), 
invokeMethod(Class, String, Object...), 
MethodInvoker, 
ReflectionUtils.makeAccessible(Method), 
ReflectionUtils.invokeMethod(Method, Object, Object[]), 
ReflectionUtils.handleReflectionException(Exception)