should throw NullPointerException when invoke mock method on null object (issue-163)

This commit is contained in:
金戟
2021-11-02 23:09:57 +08:00
parent f6206ee294
commit 990f407ebe
3 changed files with 12 additions and 4 deletions

View File

@@ -189,13 +189,13 @@ public class PrivateAccessor {
} catch (IllegalAccessException e) {
throw new MemberAccessException("Failed to access private method \"" + method + "\"", e);
} catch (NoSuchFieldException e) {
throw new MemberAccessException("Private method \"" + method + "\" not exist");
throw new MemberAccessException("Private method \"" + method + "\" not exist", e);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException) {
throw (RuntimeException)e.getTargetException();
}
throw new MemberAccessException("Invoke private method \"" + method + "\" failed with exception", e);
}
throw new MemberAccessException("Private method \"" + method + "\" not exist");
throw new MemberAccessException("Private method \"" + method + "\" not found");
}
}

View File

@@ -1,5 +1,6 @@
package com.alibaba.testable.core.util;
import com.alibaba.testable.core.exception.MemberAccessException;
import com.alibaba.testable.core.model.MockContext;
import java.util.HashSet;
@@ -69,7 +70,14 @@ public class MockAssociationUtil {
if (originMethod.equals(CONSTRUCTOR)) {
return construct(originClass, args);
} else if (args[0] == null) {
return invokeStatic(originClass, originMethod, CollectionUtil.slice(args, 1));
try {
return invokeStatic(originClass, originMethod, CollectionUtil.slice(args, 1));
} catch (RuntimeException e) {
if (e instanceof MemberAccessException && e.getCause() instanceof NoSuchFieldException) {
throw new NullPointerException("Invoking method \"" + originMethod + "\" of null object");
}
throw e;
}
} else {
return invoke(args[0], originMethod, CollectionUtil.slice(args, 1));
}