类 ReflectionTestUtils
- java.lang.Object
- org.springframework.test.util.ReflectionTestUtils
public abstract class ReflectionTestUtils extends Object
ReflectionTestUtilsis 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-
publicfield, invoke a non-publicsetter method, or invoke a non-publicconfiguration 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
staticfields andstaticmethods — for example,setField(Class, String, Object),getField(Class, String),invokeMethod(Class, String, Object...),invokeMethod(Object, Class, String, Object...), etc.- 从以下版本开始:
- 2.5
- 作者:
- Sam Brannen, Juergen Hoeller
- 另请参阅:
ReflectionUtils,AopTestUtils
- ORM frameworks such as JPA and Hibernate which condone the usage of
构造器概要
构造器 构造器 说明 ReflectionTestUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static ObjectgetField(Class<?> targetClass, String name)static ObjectgetField(Object targetObject, Class<?> targetClass, String name)static ObjectgetField(Object targetObject, String name)static 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 voidsetField(Class<?> targetClass, String name, Object value)static voidsetField(Class<?> targetClass, String name, Object value, Class<?> type)static voidsetField(Object targetObject, Class<?> targetClass, String name, Object value, Class<?> type)Set the field with the givenname/typeon the providedtargetObject/targetClassto the suppliedvalue.static voidsetField(Object targetObject, String name, Object value)static voidsetField(Object targetObject, String name, Object value, Class<?> type)
构造器详细资料
ReflectionTestUtils
public ReflectionTestUtils()
方法详细资料
setField
public static void setField(Object targetObject, String name, @Nullable Object value)
Set the field with the givennameon the providedtargetObjectto the suppliedvalue.This method delegates to
setField(Object, String, Object, Class), supplyingnullfor thetypeargument.- 参数:
targetObject- the target object on which to set the field; nevernullname- the name of the field to set; nevernullvalue- 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/typeon the providedtargetObjectto the suppliedvalue.This method delegates to
setField(Object, Class, String, Object, Class), supplyingnullfor thetargetClassargument.- 参数:
targetObject- the target object on which to set the field; nevernullname- the name of the field to set; may benulliftypeis specifiedvalue- the value to settype- the type of the field to set; may benullifnameis specified
setField
public static void setField(Class<?> targetClass, String name, @Nullable Object value)
Set 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.- 参数:
targetClass- the target class on which to set the static field; nevernullname- the name of the field to set; nevernullvalue- the value to set- 从以下版本开始:
- 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/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.- 参数:
targetClass- the target class on which to set the static field; nevernullname- the name of the field to set; may benulliftypeis specifiedvalue- the value to settype- the type of the field to set; may benullifnameis specified- 从以下版本开始:
- 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/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.- 参数:
targetObject- the target object on which to set the field; may benullif the field is statictargetClass- the target class on which to set the field; may benullif the field is an instance fieldname- the name of the field to set; may benulliftypeis specifiedvalue- the value to settype- the type of the field to set; may benullifnameis specified- 从以下版本开始:
- 4.2
- 另请参阅:
ReflectionUtils.findField(Class, String, Class),ReflectionUtils.makeAccessible(Field),ReflectionUtils.setField(Field, Object, Object),AopTestUtils.getUltimateTargetObject(Object)
getField
@Nullable public static Object getField(Object targetObject, String name)
Get the value of the field with the givennamefrom the providedtargetObject.This method delegates to
getField(Object, Class, String), supplyingnullfor thetargetClassargument.- 参数:
targetObject- the target object from which to get the field; nevernullname- the name of the field to get; nevernull- 返回:
- the field's current value
- 另请参阅:
getField(Class, String)
getField
@Nullable public static Object getField(Class<?> targetClass, String name)
Get the value of the static field with the givennamefrom the providedtargetClass.This method delegates to
getField(Object, Class, String), supplyingnullfor thetargetObjectargument.- 参数:
targetClass- the target class from which to get the static field; nevernullname- the name of the field to get; nevernull- 返回:
- the field's current value
- 从以下版本开始:
- 4.2
- 另请参阅:
getField(Object, String)
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.- 参数:
targetObject- the target object from which to get the field; may benullif the field is statictargetClass- the target class from which to get the field; may benullif the field is an instance fieldname- the name of the field to get; nevernull- 返回:
- the field's current value
- 从以下版本开始:
- 4.2
- 另请参阅:
getField(Object, String),getField(Class, String),ReflectionUtils.findField(Class, String, Class),ReflectionUtils.makeAccessible(Field),ReflectionUtils.getField(Field, Object),AopTestUtils.getUltimateTargetObject(Object)
invokeSetterMethod
public static void invokeSetterMethod(Object target, String name, Object value)
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.- 参数:
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- 另请参阅:
ReflectionUtils.findMethod(Class, String, Class[]),ReflectionUtils.makeAccessible(Method),ReflectionUtils.invokeMethod(Method, Object, Object[])
invokeSetterMethod
public 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.- 参数:
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- 另请参阅:
ReflectionUtils.findMethod(Class, String, Class[]),ReflectionUtils.makeAccessible(Method),ReflectionUtils.invokeMethod(Method, Object, Object[])
invokeGetterMethod
@Nullable public static Object invokeGetterMethod(Object target, String name)
Invoke 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.- 参数:
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- 返回:
- the value returned from the invocation
- 另请参阅:
ReflectionUtils.findMethod(Class, String, Class[]),ReflectionUtils.makeAccessible(Method),ReflectionUtils.invokeMethod(Method, Object, Object[])
invokeMethod
@Nullable public static <T> T invokeMethod(Object target, String name, Object... args)
Invoke the method with the givennameon the supplied target object with the supplied arguments.This method delegates to
invokeMethod(Object, Class, String, Object...), supplyingnullfor thetargetClassargument.- 参数:
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- 返回:
- the invocation result, if any
- 另请参阅:
invokeMethod(Class, String, Object...),invokeMethod(Object, Class, String, Object...),MethodInvoker,ReflectionUtils.makeAccessible(Method),ReflectionUtils.invokeMethod(Method, Object, Object[]),ReflectionUtils.handleReflectionException(Exception)
invokeMethod
@Nullable public static <T> T invokeMethod(Class<?> targetClass, String name, Object... args)
Invoke 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.- 参数:
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- 返回:
- the invocation result, if any
- 从以下版本开始:
- 5.2
- 另请参阅:
invokeMethod(Object, String, Object...),invokeMethod(Object, Class, String, Object...),MethodInvoker,ReflectionUtils.makeAccessible(Method),ReflectionUtils.invokeMethod(Method, Object, Object[]),ReflectionUtils.handleReflectionException(Exception)
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.- 参数:
targetObject- the target object on which to invoke the method; may benullif the method is statictargetClass- the target class on which to invoke the method; may benullif the method is an instance methodname- the name of the method to invokeargs- the arguments to provide to the method- 返回:
- the invocation result, if any
- 从以下版本开始:
- 5.2
- 另请参阅:
invokeMethod(Object, String, Object...),invokeMethod(Class, String, Object...),MethodInvoker,ReflectionUtils.makeAccessible(Method),ReflectionUtils.invokeMethod(Method, Object, Object[]),ReflectionUtils.handleReflectionException(Exception)