Support static methods with ReflectionTestUtils.invokeMethod()
Prior to this commit, the invokeMethod() utility method in ReflectionTestUtils only supported instance methods. This commit brings the invokeMethod() support on par with the getField() support by supporting the invocation of static methods via two new invokeMethod() variants. Closes gh-23504
This commit is contained in:
@@ -27,6 +27,7 @@ import org.springframework.test.util.subpackage.LegacyEntity;
|
||||
import org.springframework.test.util.subpackage.Person;
|
||||
import org.springframework.test.util.subpackage.PersonEntity;
|
||||
import org.springframework.test.util.subpackage.StaticFields;
|
||||
import org.springframework.test.util.subpackage.StaticMethods;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
@@ -57,6 +58,7 @@ class ReflectionTestUtilsTests {
|
||||
@BeforeEach
|
||||
void resetStaticFields() {
|
||||
StaticFields.reset();
|
||||
StaticMethods.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -327,7 +329,7 @@ class ReflectionTestUtilsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("[SPR-8644] findMethod() does not currently support var-args")
|
||||
@Disabled("[SPR-8644] MethodInvoker.findMatchingMethod() does not currently support var-args")
|
||||
void invokeMethodWithPrimitiveVarArgs() {
|
||||
// IntelliJ IDEA 11 won't accept int assignment here
|
||||
Integer sum = invokeMethod(component, "add", 1, 2, 3, 4);
|
||||
@@ -422,4 +424,66 @@ class ReflectionTestUtilsTests {
|
||||
assertThat(entity.toString().contains(testCollaborator)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeStaticMethodWithNullTargetClass() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> invokeMethod((Class<?>) null, null))
|
||||
.withMessage("Target class must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeStaticMethodWithNullMethodName() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> invokeMethod(getClass(), null))
|
||||
.withMessage("Method name must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeStaticMethodWithEmptyMethodName() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> invokeMethod(getClass(), " "))
|
||||
.withMessage("Method name must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokePublicStaticVoidMethodWithArguments() {
|
||||
assertThat(StaticMethods.getPublicMethodValue()).isEqualTo("public");
|
||||
|
||||
String testCollaborator = "test collaborator";
|
||||
invokeMethod(StaticMethods.class, "publicMethod", testCollaborator);
|
||||
assertThat(StaticMethods.getPublicMethodValue()).isEqualTo(testCollaborator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokePublicStaticMethodWithoutArguments() {
|
||||
assertThat(StaticMethods.getPublicMethodValue()).isEqualTo("public");
|
||||
|
||||
String result = invokeMethod(StaticMethods.class, "publicMethod");
|
||||
assertThat(result).isEqualTo(StaticMethods.getPublicMethodValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokePrivateStaticVoidMethodWithArguments() {
|
||||
assertThat(StaticMethods.getPrivateMethodValue()).isEqualTo("private");
|
||||
|
||||
String testCollaborator = "test collaborator";
|
||||
invokeMethod(StaticMethods.class, "privateMethod", testCollaborator);
|
||||
assertThat(StaticMethods.getPrivateMethodValue()).isEqualTo(testCollaborator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokePrivateStaticMethodWithoutArguments() {
|
||||
assertThat(StaticMethods.getPrivateMethodValue()).isEqualTo("private");
|
||||
|
||||
String result = invokeMethod(StaticMethods.class, "privateMethod");
|
||||
assertThat(result).isEqualTo(StaticMethods.getPrivateMethodValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeStaticMethodWithNullTargetObjectAndNullTargetClass() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> invokeMethod((Object) null, (Class<?>) null, "id"))
|
||||
.withMessage("Either 'targetObject' or 'targetClass' for the method must be specified");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.util.subpackage;
|
||||
|
||||
/**
|
||||
* Simple class with static methods; intended for use in unit tests.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 5.2
|
||||
*/
|
||||
public class StaticMethods {
|
||||
|
||||
public static String publicMethodValue = "public";
|
||||
|
||||
private static String privateMethodValue = "private";
|
||||
|
||||
|
||||
public static void publicMethod(String value) {
|
||||
publicMethodValue = value;
|
||||
}
|
||||
|
||||
public static String publicMethod() {
|
||||
return publicMethodValue;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void privateMethod(String value) {
|
||||
privateMethodValue = value;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static String privateMethod() {
|
||||
return privateMethodValue;
|
||||
}
|
||||
|
||||
public static void reset() {
|
||||
publicMethodValue = "public";
|
||||
privateMethodValue = "private";
|
||||
}
|
||||
|
||||
public static String getPublicMethodValue() {
|
||||
return publicMethodValue;
|
||||
}
|
||||
|
||||
public static String getPrivateMethodValue() {
|
||||
return privateMethodValue;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user