Add utiliy method to invoke a method on an object.

This commit is contained in:
John Blum
2018-08-29 10:06:33 -07:00
parent 2d01676f96
commit 2bd1afdb9e
2 changed files with 62 additions and 0 deletions

View File

@@ -59,6 +59,28 @@ public class ObjectUtilsUnitTests {
}
}
@Test
public void invokeMethodOnObjectReturnsValue() {
assertThat(ObjectUtils.<String>invoke(new TestObject(), "testMethod")).isEqualTo("TEST");
}
@Test(expected = IllegalArgumentException.class)
public void invokeNonExistingMethodOnObjectThrowsIllegalArgumentException() {
try {
ObjectUtils.invoke(new TestObject(), "nonExistingMethod");
}
catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Method [nonExistingMethod] on Object of type [%s] not found",
TestObject.class.getName());
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void returnValueThrowOnNullWithNonNullValueReturnsValue() {
assertThat(ObjectUtils.returnValueThrowOnNull("test")).isEqualTo("test");
@@ -78,4 +100,11 @@ public class ObjectUtilsUnitTests {
throw expected;
}
}
public static class TestObject {
public String testMethod() {
return "TEST";
}
}
}