Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Thunderforge opened this issue Aug 22, 2019 · 1 comment
Closed
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@Thunderforge
Copy link

Thunderforge commented Aug 22, 2019

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.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 22, 2019
@sbrannen sbrannen self-assigned this Aug 23, 2019
@sbrannen sbrannen added in: test Issues in the test module type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 23, 2019
@sbrannen sbrannen added this to the 5.2 RC2 milestone Aug 23, 2019
@sbrannen
Copy link
Member

The necessary changes have been made for Spring Framework 5.2 in 5ce75f3.

Thanks for raising the issue.

sbrannen added a commit that referenced this issue Aug 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants