Skip to content

Support invoking static methods with ReflectionTestUtils.invokeMethod() #23504

Closed
@Thunderforge

Description

@Thunderforge

Background

Spring Test's ReflectionTestUtils includes methods for setting/getting fields, like so (bolded text is from me):

  • getField(Class<?> targetClass, String name)
    • Get the value of the static field with the given name from the provided targetClass.
  • getField(Object targetObject, Class<?> targetClass, String name)
    • Get the value of the field with the given name from the provided targetObject/targetClass.
  • getField(Object targetObject, String name)
    • Get the value of the field with the given name from the provided targetObject.

Note that getField() works for both static fields and regular fields (support for static fields was added in v4.2.0.RC1 with this commit, associated with #11458).

Problem

In contrast, there is only one version of invokeMethod(), and it only works on an instantiated Object.

  • invokeMethod(Object target, String name, Object... args)
    • Invoke the method with the given name on the supplied target object with the supplied arguments.

There are no other versions of invokeMethod() that work for static methods.

Feature Request

To make the class more consistent with getField() and setField(), I would like to request a new version of invokeMethod() that works on static methods:

  • invokeMethod(Class<?> targetClass, String name, Object... args)
    • Invoke the static method with the given name on the provided targetClass with the supplied arguments.

If it makes sense, we should also add:

  • invokeMethod(Object targetObject, Class<?> targetClass, String name, Object... args)
    • Invoke the method with the given name on the provided targetObject/targetClass with the supplied arguments.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions