Class ReflectionTestUtils
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:
 
- ORM frameworks such as JPA and Hibernate which condone the usage of
 privateorprotectedfield access as opposed topublicsetter methods for properties in a domain entity.
- Spring's support for annotations such as
 @Autowired,@Inject, and@Resourcewhich provides dependency injection forprivateorprotectedfields, setter methods, and configuration methods.
- Use of annotations such as @PostConstructand@PreDestroyfor 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.
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic Objectstatic Objectstatic Objectstatic ObjectinvokeGetterMethod(Object target, String name) Invoke the getter method with the givennameon the supplied target object with the suppliedvalue.static <T> TinvokeMethod(Class<?> targetClass, String name, Object... args) Invoke the static method with the givennameon the supplied target class with the supplied arguments.static <T> TinvokeMethod(Object targetObject, Class<?> targetClass, String name, Object... args) Invoke the method with the givennameon the providedtargetObject/targetClasswith the supplied arguments.static <T> TinvokeMethod(Object target, String name, Object... args) Invoke the method with the givennameon the supplied target object with the supplied arguments.static voidinvokeSetterMethod(Object target, String name, Object value) Invoke the setter method with the givennameon the supplied target object with the suppliedvalue.static voidinvokeSetterMethod(Object target, String name, Object value, Class<?> type) Invoke the setter method with the givennameon the supplied target object with the suppliedvalue.static voidstatic voidstatic voidSet the field with the givenname/typeon the providedtargetObject/targetClassto the suppliedvalue.static voidstatic void
- 
Constructor Details- 
ReflectionTestUtilspublic ReflectionTestUtils()
 
- 
- 
Method Details- 
setFieldSet the field with the givennameon the providedtargetObjectto the suppliedvalue.This method delegates to setField(Object, String, Object, Class), supplyingnullfor thetypeargument.- Parameters:
- targetObject- the target object on which to set the field; never- null
- name- the name of the field to set; never- null
- value- the value to set
 
- 
setFieldpublic static void setField(Object targetObject, @Nullable String name, @Nullable Object value, @Nullable Class<?> type) Set the field with the givenname/typeon the providedtargetObjectto the suppliedvalue.This method delegates to setField(Object, Class, String, Object, Class), supplyingnullfor thetargetClassargument.- Parameters:
- targetObject- the target object on which to set the field; never- null
- name- the name of the field to set; may be- nullif- typeis specified
- value- the value to set
- type- the type of the field to set; may be- nullif- nameis specified
 
- 
setFieldSet the static field with the givennameon the providedtargetClassto the suppliedvalue.This method delegates to setField(Object, Class, String, Object, Class), supplyingnullfor thetargetObjectandtypearguments.This method does not support setting static finalfields.- Parameters:
- targetClass- the target class on which to set the static field; never- null
- name- the name of the field to set; never- null
- value- the value to set
- Since:
- 4.2
 
- 
setFieldpublic static void setField(Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type) Set the static field with the givenname/typeon the providedtargetClassto the suppliedvalue.This method delegates to setField(Object, Class, String, Object, Class), supplyingnullfor thetargetObjectargument.This method does not support setting static finalfields.- Parameters:
- targetClass- the target class on which to set the static field; never- null
- name- the name of the field to set; may be- nullif- typeis specified
- value- the value to set
- type- the type of the field to set; may be- nullif- nameis specified
- Since:
- 4.2
 
- 
setFieldpublic static void setField(@Nullable Object targetObject, @Nullable Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type) Set the field with the givenname/typeon the providedtargetObject/targetClassto the suppliedvalue.If the supplied targetObjectis 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- publicfields accessible, thus allowing one to setprotected,private, and package-private fields.This method does not support setting static finalfields.- Parameters:
- targetObject- the target object on which to set the field; may be- nullif the field is static
- targetClass- the target class on which to set the field; may be- nullif the field is an instance field
- name- the name of the field to set; may be- nullif- typeis specified
- value- the value to set
- type- the type of the field to set; may be- nullif- nameis specified
- Since:
- 4.2
- See Also:
 
- 
getFieldGet the value of the field with the givennamefrom the providedtargetObject.This method delegates to getField(Object, Class, String), supplyingnullfor thetargetClassargument.- Parameters:
- targetObject- the target object from which to get the field; never- null
- name- the name of the field to get; never- null
- Returns:
- the field's current value
- See Also:
 
- 
getFieldGet the value of the static field with the givennamefrom the providedtargetClass.This method delegates to getField(Object, Class, String), supplyingnullfor thetargetObjectargument.- Parameters:
- targetClass- the target class from which to get the static field; never- null
- name- the name of the field to get; never- null
- Returns:
- the field's current value
- Since:
- 4.2
- See Also:
 
- 
getField@Nullable public static Object getField(@Nullable Object targetObject, @Nullable Class<?> targetClass, String name) Get the value of the field with the givennamefrom the providedtargetObject/targetClass.If the supplied targetObjectis 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- publicfields accessible, thus allowing one to getprotected,private, and package-private fields.- Parameters:
- targetObject- the target object from which to get the field; may be- nullif the field is static
- targetClass- the target class from which to get the field; may be- nullif the field is an instance field
- name- the name of the field to get; never- null
- Returns:
- the field's current value
- Since:
- 4.2
- See Also:
 
- 
invokeSetterMethodInvoke the setter method with the givennameon the supplied target object with the suppliedvalue.This method traverses the class hierarchy in search of the desired method. In addition, an attempt will be made to make non- publicmethods accessible, thus allowing one to invokeprotected,private, and package-private setter methods.In addition, this method supports JavaBean-style property names. For example, if you wish to set the nameproperty on the target object, you may pass either "name" or "setName" as the method name.- Parameters:
- target- the target object on which to invoke the specified setter method
- name- the name of the setter method to invoke or the corresponding property name
- value- the value to provide to the setter method
- See Also:
 
- 
invokeSetterMethodpublic static void invokeSetterMethod(Object target, String name, @Nullable Object value, @Nullable Class<?> type) Invoke the setter method with the givennameon the supplied target object with the suppliedvalue.This method traverses the class hierarchy in search of the desired method. In addition, an attempt will be made to make non- publicmethods accessible, thus allowing one to invokeprotected,private, and package-private setter methods.In addition, this method supports JavaBean-style property names. For example, if you wish to set the nameproperty on the target object, you may pass either "name" or "setName" as the method name.- Parameters:
- target- the target object on which to invoke the specified setter method
- name- the name of the setter method to invoke or the corresponding property name
- value- the value to provide to the setter method
- type- the formal parameter type declared by the setter method
- See Also:
 
- 
invokeGetterMethodInvoke the getter method with the givennameon the supplied target object with the suppliedvalue.This method traverses the class hierarchy in search of the desired method. In addition, an attempt will be made to make non- publicmethods accessible, thus allowing one to invokeprotected,private, and package-private getter methods.In addition, this method supports JavaBean-style property names. For example, if you wish to get the nameproperty on the target object, you may pass either "name" or "getName" as the method name.- Parameters:
- target- the target object on which to invoke the specified getter method
- name- the name of the getter method to invoke or the corresponding property name
- Returns:
- the value returned from the invocation
- See Also:
 
- 
invokeMethodInvoke the method with the givennameon the supplied target object with the supplied arguments.This method delegates to invokeMethod(Object, Class, String, Object...), supplyingnullfor thetargetClassargument.- Parameters:
- target- the target object on which to invoke the specified method
- name- the name of the method to invoke
- args- the arguments to provide to the method
- Returns:
- the invocation result, if any
- See Also:
 
- 
invokeMethodInvoke the static method with the givennameon the supplied target class with the supplied arguments.This method delegates to invokeMethod(Object, Class, String, Object...), supplyingnullfor thetargetObjectargument.- Parameters:
- targetClass- the target class on which to invoke the specified method
- name- the name of the method to invoke
- args- the arguments to provide to the method
- Returns:
- the invocation result, if any
- Since:
- 5.2
- See Also:
 
- 
invokeMethod@Nullable public static <T> T invokeMethod(@Nullable Object targetObject, @Nullable Class<?> targetClass, String name, Object... args) Invoke the method with the givennameon the providedtargetObject/targetClasswith 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- publicmethods accessible, thus allowing one to invokeprotected,private, and package-private methods.- Parameters:
- targetObject- the target object on which to invoke the method; may be- nullif the method is static
- targetClass- the target class on which to invoke the method; may be- nullif the method is an instance method
- name- the name of the method to invoke
- args- the arguments to provide to the method
- Returns:
- the invocation result, if any
- Since:
- 5.2
- See Also:
 
 
-