org.springframework.test.util

Class ReflectionTestUtils

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


  • public abstract class ReflectionTestUtils
    extends java.lang.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 — for example, setField(Class, String, Object), getField(Class, String), etc.

    Since:
    2.5
    Author:
    Sam Brannen, Juergen Hoeller
    See Also:
    ReflectionUtils, AopTestUtils
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static java.lang.Object getField(java.lang.Class<?> targetClass, java.lang.String name)
      Get the value of the static field with the given name from the provided targetClass.
      static java.lang.Object getField(java.lang.Object targetObject, java.lang.Class<?> targetClass, java.lang.String name)
      Get the value of the field with the given name from the provided targetObject/targetClass.
      static java.lang.Object getField(java.lang.Object targetObject, java.lang.String name)
      Get the value of the field with the given name from the provided targetObject.
      static java.lang.Object invokeGetterMethod(java.lang.Object target, java.lang.String name)
      Invoke the getter method with the given name on the supplied target object with the supplied value.
      static <T> T invokeMethod(java.lang.Object target, java.lang.String name, java.lang.Object... args)
      Invoke the method with the given name on the supplied target object with the supplied arguments.
      static void invokeSetterMethod(java.lang.Object target, java.lang.String name, java.lang.Object value)
      Invoke the setter method with the given name on the supplied target object with the supplied value.
      static void invokeSetterMethod(java.lang.Object target, java.lang.String name, java.lang.Object value, java.lang.Class<?> type)
      Invoke the setter method with the given name on the supplied target object with the supplied value.
      static void setField(java.lang.Class<?> targetClass, java.lang.String name, java.lang.Object value)
      Set the static field with the given name on the provided targetClass to the supplied value.
      static void setField(java.lang.Class<?> targetClass, java.lang.String name, java.lang.Object value, java.lang.Class<?> type)
      Set the static field with the given name/type on the provided targetClass to the supplied value.
      static void setField(java.lang.Object targetObject, java.lang.Class<?> targetClass, java.lang.String name, java.lang.Object value, java.lang.Class<?> type)
      Set the field with the given name/type on the provided targetObject/targetClass to the supplied value.
      static void setField(java.lang.Object targetObject, java.lang.String name, java.lang.Object value)
      Set the field with the given name on the provided targetObject to the supplied value.
      static void setField(java.lang.Object targetObject, java.lang.String name, java.lang.Object value, java.lang.Class<?> type)
      Set the field with the given name/type on the provided targetObject to the supplied value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ReflectionTestUtils

        public ReflectionTestUtils()
    • Method Detail

      • setField

        public static void setField(java.lang.Object targetObject,
                                    java.lang.String name,
                                    @Nullable
                                    java.lang.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(java.lang.Object targetObject,
                                    @Nullable
                                    java.lang.String name,
                                    @Nullable
                                    java.lang.Object value,
                                    @Nullable
                                    java.lang.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(java.lang.Class<?> targetClass,
                                    java.lang.String name,
                                    @Nullable
                                    java.lang.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.

        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(java.lang.Class<?> targetClass,
                                    @Nullable
                                    java.lang.String name,
                                    @Nullable
                                    java.lang.Object value,
                                    @Nullable
                                    java.lang.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.

        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
                                    java.lang.Object targetObject,
                                    @Nullable
                                    java.lang.Class<?> targetClass,
                                    @Nullable
                                    java.lang.String name,
                                    @Nullable
                                    java.lang.Object value,
                                    @Nullable
                                    java.lang.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.

        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:
        ReflectionUtils.findField(Class, String, Class), ReflectionUtils.makeAccessible(Field), ReflectionUtils.setField(Field, Object, Object), AopTestUtils.getUltimateTargetObject(Object)
      • getField

        @Nullable
        public static java.lang.Object getField(java.lang.Object targetObject,
                                                          java.lang.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(Class, String)
      • getField

        @Nullable
        public static java.lang.Object getField(java.lang.Class<?> targetClass,
                                                          java.lang.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(Object, String)
      • invokeSetterMethod

        public static void invokeSetterMethod(java.lang.Object target,
                                              java.lang.String name,
                                              java.lang.Object value)
        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.

        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 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:
        ReflectionUtils.findMethod(Class, String, Class[]), ReflectionUtils.makeAccessible(Method), ReflectionUtils.invokeMethod(Method, Object, Object[])
      • invokeSetterMethod

        public static void invokeSetterMethod(java.lang.Object target,
                                              java.lang.String name,
                                              @Nullable
                                              java.lang.Object value,
                                              @Nullable
                                              java.lang.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.

        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 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:
        ReflectionUtils.findMethod(Class, String, Class[]), ReflectionUtils.makeAccessible(Method), ReflectionUtils.invokeMethod(Method, Object, Object[])
      • invokeGetterMethod

        @Nullable
        public static java.lang.Object invokeGetterMethod(java.lang.Object target,
                                                                    java.lang.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.

        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 method
        name - the name of the getter method to invoke or the corresponding property name
        Returns:
        the value returned from the invocation
        See Also:
        ReflectionUtils.findMethod(Class, String, Class[]), ReflectionUtils.makeAccessible(Method), ReflectionUtils.invokeMethod(Method, Object, Object[])
      • invokeMethod

        @Nullable
        public static <T> T invokeMethod(java.lang.Object target,
                                                   java.lang.String name,
                                                   java.lang.Object... args)
        Invoke the method with the given name on the supplied target object 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.

        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:
        MethodInvoker, ReflectionUtils.makeAccessible(Method), ReflectionUtils.invokeMethod(Method, Object, Object[]), ReflectionUtils.handleReflectionException(Exception)