Class ReflectionTestUtils

java.lang.Object
org.springframework.test.util.ReflectionTestUtils

public abstract 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:

  • ORM frameworks such as JPA and Hibernate which condone the usage of private or protected field access as opposed to public setter methods for properties in a domain entity.
  • Spring's support for annotations such as @Autowired, @Inject, and @Resource which provides dependency injection for private or protected 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 Details

    • ReflectionTestUtils

      public ReflectionTestUtils()
  • Method Details

    • setField

      public static void setField(Object targetObject, String name, @Nullable Object value)
      Set the field with the given name on the provided targetObject to the supplied value.

      This method delegates to setField(Object, String, Object, Class), supplying null for the type argument.

      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
    • setField

      public static void setField(Object targetObject, @Nullable String name, @Nullable Object value, @Nullable Class<?> type)
      Set the field with the given name/type on the provided targetObject to the supplied value.

      This method delegates to setField(Object, Class, String, Object, Class), supplying null for the targetClass argument.

      Parameters:
      targetObject - the target object on which to set the field; never null
      name - the name of the field to set; may be null if type is specified
      value - the value to set
      type - the type of the field to set; may be null if name is specified
    • setField

      public static void setField(Class<?> targetClass, String name, @Nullable Object value)
      Set the static field with the given name on the provided targetClass to the supplied value.

      This method delegates to setField(Object, Class, String, Object, Class), supplying null for the targetObject and type arguments.

      This method does not support setting static final fields.

      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
    • setField

      public static void setField(Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type)
      Set the static field with the given name/type on the provided targetClass to the supplied value.

      This method delegates to setField(Object, Class, String, Object, Class), supplying null for the targetObject argument.

      This method does not support setting static final fields.

      Parameters:
      targetClass - the target class on which to set the static field; never null
      name - the name of the field to set; may be null if type is specified
      value - the value to set
      type - the type of the field to set; may be null if name 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 given name/type on the provided targetObject/targetClass to the supplied value.

      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 set protected, 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 be null if the field is static
      targetClass - the target class on which to set the field; may be null if the field is an instance field
      name - the name of the field to set; may be null if type is specified
      value - the value to set
      type - the type of the field to set; may be null if name is specified
      Since:
      4.2
      See Also:
    • getField

      @Nullable public static Object getField(Object targetObject, String name)
      Get the value of the field with the given name from the provided targetObject.

      This method delegates to getField(Object, Class, String), supplying null for the targetClass argument.

      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:
    • getField

      @Nullable public static Object getField(Class<?> targetClass, String name)
      Get the value of the static field with the given name from the provided targetClass.

      This method delegates to getField(Object, Class, String), supplying null for the targetObject argument.

      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 given name from the provided targetObject/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 get protected, private, and package-private fields.

      Parameters:
      targetObject - the target object from which to get the field; may be null if the field is static
      targetClass - the target class from which to get the field; may be null if 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:
    • invokeSetterMethod

      public 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.

      This method delegates to invokeSetterMethod(Object, String, Object, Class), supplying null for the parameter type.

      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
    • invokeSetterMethod

      public static void invokeSetterMethod(Object target, String name, @Nullable Object value, @Nullable Class<?> type)
      Invoke 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.

      This method also 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.

      As of Spring Framework 6.2, if the supplied target object is a CGLIB proxy which does not intercept the setter method, the proxy will be unwrapped allowing the setter method to be invoked directly on the ultimate target of the proxy.

      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 (may be null to indicate any type)
      See Also:
    • invokeGetterMethod

      @Nullable public static Object invokeGetterMethod(Object target, String name)
      Invoke the getter 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 getter methods.

      This method also 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.

      As of Spring Framework 6.2, if the supplied target object is a CGLIB proxy which does not intercept the getter method, the proxy will be unwrapped allowing the getter method to be invoked directly on the ultimate target of the proxy.

      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:
    • invokeMethod

      @Nullable public 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.

      This method delegates to invokeMethod(Object, Class, String, Object...), supplying null for the targetClass argument.

      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:
    • invokeMethod

      @Nullable public static <T> T invokeMethod(Class<?> targetClass, String name, Object... args)
      Invoke the static method with the given name on the supplied target class with the supplied arguments.

      This method delegates to invokeMethod(Object, Class, String, Object...), supplying null for the targetObject argument.

      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 given name on the provided targetObject/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 invoke protected, private, and package-private methods.

      As of Spring Framework 6.2, if the supplied target object is a CGLIB proxy which does not intercept the method, the proxy will be unwrapped allowing the method to be invoked directly on the ultimate target of the proxy.

      Parameters:
      targetObject - the target object on which to invoke the method; may be null if the method is static
      targetClass - the target class on which to invoke the method; may be null if 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: