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

@ -139,7 +139,7 @@ public class SourceClassHandler extends BaseClassHandler {
for (MethodInfo m : memberInjectMethods) {
String nodeOwner = ClassUtil.fitCompanionClassName(node.owner);
String nodeName = ClassUtil.fitKotlinAccessorName(node.name);
// Kotlin accessor method will append a extra type parameter
// Kotlin accessor method will append an extra type parameter
String nodeDesc = nodeName.equals(node.name) ? node.desc : MethodUtil.removeFirstParameter(node.desc);
if (m.getClazz().equals(nodeOwner) && m.getName().equals(nodeName) && m.getDesc().equals(nodeDesc)) {
return m;

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));
}