spring-framework / org.springframework.test.util / ReflectionTestUtils

ReflectionTestUtils

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

In addition, several methods in this class provide support for static fields — for example, #setField(Class, String, Object), #getField(Class, String), etc.

Author
Sam Brannen

Author
Juergen Hoeller

Since
2.5

See Also
ReflectionUtilsAopTestUtils

Constructors

<init>

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 or protected field access as opposed to public setter methods for properties in a domain entity.
  • Spring's support for annotations such as org.springframework.beans.factory.annotation.Autowired, javax.inject.Inject, and javax.annotation.Resource which provides dependency injection for private or protected fields, setter methods, and configuration methods.
  • Use of annotations such as javax.annotation.PostConstruct and javax.annotation.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.

Functions

getField

open static fun getField(targetObject: Any, name: String): Any

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.

open static fun getField(targetClass: Class<*>, name: String): Any

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.

open static fun getField(targetObject: Any, targetClass: Class<*>, name: String): Any

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.

invokeGetterMethod

open static fun invokeGetterMethod(target: Any, name: String): Any

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.

invokeMethod

open static fun <T : Any> invokeMethod(target: Any, name: String, vararg args: Any): T

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.

invokeSetterMethod

open static fun invokeSetterMethod(target: Any, name: String, value: Any): Unit
open static fun invokeSetterMethod(target: Any, name: String, value: Any, type: Class<*>): Unit

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.

setField

open static fun setField(targetObject: Any, name: String, value: Any): Unit

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.

open static fun setField(targetObject: Any, name: String, value: Any, type: Class<*>): Unit

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.

open static fun setField(targetClass: Class<*>, name: String, value: Any): Unit

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.

open static fun setField(targetClass: Class<*>, name: String, value: Any, type: Class<*>): Unit

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.

open static fun setField(targetObject: Any, targetClass: Class<*>, name: String, value: Any, type: Class<*>): Unit

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.