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
private
orprotected
field access as opposed topublic
setter methods for properties in a domain entity. - Spring's support for annotations such as
@Autowired
,@Inject
, and@Resource
which provides dependency injection forprivate
orprotected
fields, setter methods, and configuration methods. - Use of annotations such as
@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.
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object
static Object
static Object
static Object
invokeGetterMethod
(Object target, String name) Invoke the getter method with the givenname
on the supplied target object with the suppliedvalue
.static <T> T
invokeMethod
(Class<?> targetClass, String name, Object... args) Invoke the static method with the givenname
on 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 givenname
on the providedtargetObject
/targetClass
with the supplied arguments.static <T> T
invokeMethod
(Object target, String name, Object... args) Invoke the method with the givenname
on the supplied target object with the supplied arguments.static void
invokeSetterMethod
(Object target, String name, Object value) Invoke the setter method with the givenname
on the supplied target object with the suppliedvalue
.static void
invokeSetterMethod
(Object target, String name, Object value, Class<?> type) Invoke the setter method with the givenname
on the supplied target object with the suppliedvalue
.static void
static void
static void
Set the field with the givenname
/type
on the providedtargetObject
/targetClass
to the suppliedvalue
.static void
static void
-
Constructor Details
-
ReflectionTestUtils
public ReflectionTestUtils()
-
-
Method Details
-
setField
Set the field with the givenname
on the providedtargetObject
to the suppliedvalue
.This method delegates to
setField(Object, String, Object, Class)
, supplyingnull
for thetype
argument.- Parameters:
targetObject
- the target object on which to set the field; nevernull
name
- the name of the field to set; nevernull
value
- the value to set
-
setField
public static void setField(Object targetObject, @Nullable String name, @Nullable Object value, @Nullable Class<?> type) Set the field with the givenname
/type
on the providedtargetObject
to the suppliedvalue
.This method delegates to
setField(Object, Class, String, Object, Class)
, supplyingnull
for thetargetClass
argument.- Parameters:
targetObject
- the target object on which to set the field; nevernull
name
- the name of the field to set; may benull
iftype
is specifiedvalue
- the value to settype
- the type of the field to set; may benull
ifname
is specified
-
setField
Set the static field with the givenname
on the providedtargetClass
to the suppliedvalue
.This method delegates to
setField(Object, Class, String, Object, Class)
, supplyingnull
for thetargetObject
andtype
arguments.This method does not support setting
static final
fields.- Parameters:
targetClass
- the target class on which to set the static field; nevernull
name
- the name of the field to set; nevernull
value
- the value to set- Since:
- 4.2
-
setField
public static void setField(Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type) Set the static field with the givenname
/type
on the providedtargetClass
to the suppliedvalue
.This method delegates to
setField(Object, Class, String, Object, Class)
, supplyingnull
for thetargetObject
argument.This method does not support setting
static final
fields.- Parameters:
targetClass
- the target class on which to set the static field; nevernull
name
- the name of the field to set; may benull
iftype
is specifiedvalue
- the value to settype
- the type of the field to set; may benull
ifname
is specified- Since:
- 4.2
-
setField
public static void setField(@Nullable Object targetObject, @Nullable Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type) Set the field with the givenname
/type
on the providedtargetObject
/targetClass
to the suppliedvalue
.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 setprotected
,private
, and package-private fields.This method does not support setting
static final
fields.- Parameters:
targetObject
- the target object on which to set the field; may benull
if the field is statictargetClass
- the target class on which to set the field; may benull
if the field is an instance fieldname
- the name of the field to set; may benull
iftype
is specifiedvalue
- the value to settype
- the type of the field to set; may benull
ifname
is specified- Since:
- 4.2
- See Also:
-
getField
Get the value of the field with the givenname
from the providedtargetObject
.This method delegates to
getField(Object, Class, String)
, supplyingnull
for thetargetClass
argument.- Parameters:
targetObject
- the target object from which to get the field; nevernull
name
- the name of the field to get; nevernull
- Returns:
- the field's current value
- See Also:
-
getField
Get the value of the static field with the givenname
from the providedtargetClass
.This method delegates to
getField(Object, Class, String)
, supplyingnull
for thetargetObject
argument.- Parameters:
targetClass
- the target class from which to get the static field; nevernull
name
- the name of the field to get; nevernull
- 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 givenname
from the providedtargetObject
/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 getprotected
,private
, and package-private fields.- Parameters:
targetObject
- the target object from which to get the field; may benull
if the field is statictargetClass
- the target class from which to get the field; may benull
if the field is an instance fieldname
- the name of the field to get; nevernull
- Returns:
- the field's current value
- Since:
- 4.2
- See Also:
-
invokeSetterMethod
Invoke the setter method with the givenname
on 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-
public
methods 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
name
property 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 methodname
- the name of the setter method to invoke or the corresponding property namevalue
- the value to provide to the setter method- See Also:
-
invokeSetterMethod
public static void invokeSetterMethod(Object target, String name, @Nullable Object value, @Nullable Class<?> type) Invoke the setter method with the givenname
on 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-
public
methods 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
name
property 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 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 method- See Also:
-
invokeGetterMethod
Invoke the getter method with the givenname
on 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-
public
methods 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
name
property 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 methodname
- the name of the getter method to invoke or the corresponding property name- Returns:
- the value returned from the invocation
- See Also:
-
invokeMethod
Invoke the method with the givenname
on the supplied target object with the supplied arguments.This method delegates to
invokeMethod(Object, Class, String, Object...)
, supplyingnull
for thetargetClass
argument.- Parameters:
target
- the target object on which to invoke the specified methodname
- the name of the method to invokeargs
- the arguments to provide to the method- Returns:
- the invocation result, if any
- See Also:
-
invokeMethod
Invoke the static method with the givenname
on the supplied target class with the supplied arguments.This method delegates to
invokeMethod(Object, Class, String, Object...)
, supplyingnull
for thetargetObject
argument.- Parameters:
targetClass
- the target class on which to invoke the specified methodname
- the name of the method to invokeargs
- 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 givenname
on the providedtargetObject
/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 invokeprotected
,private
, and package-private methods.- Parameters:
targetObject
- the target object on which to invoke the method; may benull
if the method is statictargetClass
- the target class on which to invoke the method; may benull
if the method is an instance methodname
- the name of the method to invokeargs
- the arguments to provide to the method- Returns:
- the invocation result, if any
- Since:
- 5.2
- See Also:
-