mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-19 09:43:29 +08:00
should throw NullPointerException when invoke mock method on null object (issue-163)
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user