|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.test.util.ReflectionTestUtils
public class ReflectionTestUtils
ReflectionTestUtils is a collection of reflection-based utility methods for use in unit and integration testing scenarios.
There are often situations in which it would be beneficial to be able to set
a non-public
field or invoke a non-public
setter 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.
ReflectionUtils
Constructor Summary | |
---|---|
ReflectionTestUtils()
|
Method Summary | |
---|---|
static void |
invokeSetterMethod(Object target,
String name,
Object value,
Class type)
Invokes 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,
Class type)
Sets the field with the given name on the
provided target object to the supplied value . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ReflectionTestUtils()
Method Detail |
---|
public static final void setField(Object target, String name, Object value, Class type) throws Exception
Sets the 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 field.name
- The name of the field to set.value
- The value to set; may be null
unless the
field type is a primitive type.type
- The type of the field.
IllegalArgumentException
- if the target object or field type is
null
; if the field name is empty; or
if the field could not be found on the target object.
Exception
- Allows all other exceptions to propagate.ReflectionUtils.findField(Class, String, Class)
,
ReflectionUtils.makeAccessible(Field)
,
ReflectionUtils.setField(Field, Object, Object)
public static final void invokeSetterMethod(Object target, String name, Object value, Class type) throws Exception
Invokes the setter method
with the given 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
method.name
- The name of the setter method to invoke or the corresponding
property name.value
- The value to provide to the setter method; may be
null
unless the parameter type is a primitive type.type
- The formal parameter type declared by the setter method.
IllegalArgumentException
- if the target object or parameter type
is null
; if the name is empty; or if
the method could not be found on the target object.
Exception
- Allows all other exceptions to propagate.ReflectionUtils.findMethod(Class, String, Class[])
,
ReflectionUtils.makeAccessible(Method)
,
ReflectionUtils.invokeMethod(Method, Object, Object[])
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |