|
|
@@ -17,7 +17,51 @@ import net.ranides.assira.reflection.mockup.ForMethodUtils.*;
|
|
|
*/
|
|
|
public class MethodUtilsTest {
|
|
|
|
|
|
- public MethodUtilsTest() {
|
|
|
+ @Test
|
|
|
+ public void testInvoke() {
|
|
|
+ String error1 = "java.lang.IllegalArgumentException: object is not an instance of declaring class";
|
|
|
+ String error2 = "java.lang.IllegalArgumentException: argument type mismatch";
|
|
|
+ String error3 = "java.lang.IllegalArgumentException: wrong number of arguments";
|
|
|
+ String error4 = "java.lang.AssertionError: non-static method require this!=null";
|
|
|
+ String error5 = "java.lang.NumberFormatException: NFE";
|
|
|
+
|
|
|
+
|
|
|
+ Method r2 = $method(Root.class, "fun2");
|
|
|
+ Method b2 = $method(B.class, "fun2");
|
|
|
+ Method b3 = $method(B.class, "fun3");
|
|
|
+
|
|
|
+ Object r = new Root();
|
|
|
+ Object b = new B();
|
|
|
+ Object c1 = new C("c1");
|
|
|
+ Object c2 = new C("c2");
|
|
|
+
|
|
|
+ assertEquals(null, MethodUtils.invoke(r2, r, 33));
|
|
|
+ assertEquals("hello", MethodUtils.invoke(r2, b, 33));
|
|
|
+ assertEquals("c1#8", MethodUtils.invoke(r2, c1, 8));
|
|
|
+ assertEquals("c2#9", MethodUtils.invoke(r2, c2, 9));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ assertThrows(e -> error1.equals(e.toString()), () -> {
|
|
|
+ MethodUtils.invoke(b2, r, 33);
|
|
|
+ });
|
|
|
+ assertThrows(e -> error1.equals(e.toString()), () -> {
|
|
|
+ MethodUtils.invoke(b2, c1, 8);
|
|
|
+ });
|
|
|
+ assertEquals("hello", MethodUtils.invoke(b2, b, 33));
|
|
|
+
|
|
|
+ assertThrows(e -> error5.equals(e.toString()), () -> {
|
|
|
+ MethodUtils.invoke(b3, b, 'a', 'b');
|
|
|
+ });
|
|
|
+ assertThrows(e -> error2.equals(e.toString()), () -> {
|
|
|
+ MethodUtils.invoke(b3, b, 'a', 5);
|
|
|
+ });
|
|
|
+ assertThrows(e -> error3.equals(e.toString()), () -> {
|
|
|
+ MethodUtils.invoke(b3, b, 'a', 'b', 'c');
|
|
|
+ });
|
|
|
+ assertThrows(e -> error4.equals(e.toString()), () -> {
|
|
|
+ MethodUtils.invoke(b3, null, 'a', 'b', 'c');
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
@@ -80,12 +124,31 @@ public class MethodUtilsTest {
|
|
|
public void fun1(int a) { }
|
|
|
|
|
|
@Override
|
|
|
- public String fun2(int a) { return null; }
|
|
|
+ public String fun2(int a) {
|
|
|
+ return "hello";
|
|
|
+ }
|
|
|
|
|
|
- public void fun3(int x, char y) {}
|
|
|
+ public void fun3(int x, char y) {
|
|
|
+ throw new NumberFormatException("NFE");
|
|
|
+ }
|
|
|
|
|
|
void fun4(String z) { }
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public static class C extends Root {
|
|
|
+
|
|
|
+ private final String text;
|
|
|
+
|
|
|
+ public C(String text) {
|
|
|
+ this.text = text;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String fun2(int a) {
|
|
|
+ return text + "#" + a;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|