public 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 and
 @Resource which provides dependency
 injection for private or protected fields, setter methods,
 and configuration methods.@PostConstruct
 and @PreDestroy for lifecycle callback
 methods.ReflectionUtils| Constructor and Description | 
|---|
ReflectionTestUtils()  | 
| Modifier and Type | Method and Description | 
|---|---|
static Object | 
getField(Object target,
        String name)
Get the field with the given  
name from the provided target object. | 
static Object | 
invokeGetterMethod(Object target,
                  String name)
Invoke the getter method with the given  
name on the supplied
 target object with the supplied value. | 
static <T> T | 
invokeMethod(Object target,
            String name,
            Object... args)
Invoke the method with the given  
name on the supplied target
 object with the supplied arguments. | 
static void | 
invokeSetterMethod(Object target,
                  String name,
                  Object value)
Invoke the setter method with the given  
name on the supplied
 target object with the supplied value. | 
static void | 
invokeSetterMethod(Object target,
                  String name,
                  Object value,
                  Class<?> type)
Invoke the setter method with the given  
name on the supplied
 target object with the supplied value. | 
static void | 
setField(Object target,
        String name,
        Object value)
 | 
static void | 
setField(Object target,
        String name,
        Object value,
        Class<?> type)
 | 
public static void setField(Object target, String name, Object value)
field with the given name on the provided
 target object to the supplied value.
 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.
target - the target object on which to set the fieldname - the name of the field to setvalue - the value to setReflectionUtils.findField(Class, String, Class), 
ReflectionUtils.makeAccessible(Field), 
ReflectionUtils.setField(Field, Object, Object)public static void setField(Object target, String name, Object value, Class<?> type)
field with the given name on the provided
 target object to the supplied value.
 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.
target - the target object on which to set the fieldname - the name of the field to setvalue - the value to settype - the type of the field (may be null)ReflectionUtils.findField(Class, String, Class), 
ReflectionUtils.makeAccessible(Field), 
ReflectionUtils.setField(Field, Object, Object)public static Object getField(Object target, String name)
name from the provided target object.
 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.
target - the target object on which to set the fieldname - the name of the field to getReflectionUtils.findField(Class, String, Class), 
ReflectionUtils.makeAccessible(Field), 
ReflectionUtils.setField(Field, Object, 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, Object value, 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[])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[])public static <T> T invokeMethod(Object target, String name, Object... args)
name on the supplied target
 object 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.
target - the target object on which to invoke the specified methodname - the name of the method to invokeargs - the arguments to provide to the methodMethodInvoker, 
ReflectionUtils.makeAccessible(Method), 
ReflectionUtils.invokeMethod(Method, Object, Object[]), 
ReflectionUtils.handleReflectionException(Exception)