问题现象
最近在使用PowerMockito进行单元测试时,遇到了一个问题,代码如下:
原方法签名
ContactInfo getInfoById(long id);
测试用例中
PowerMockito.when(mapper.getInfoById(any())).thenReturn(new ContactInfo());
这是要mock一个方法返回一个ContactInfo
对象,非常简单,但是在运行测试用例的时候,出现了如下错误:
java.lang.NullPointerException
(和业务相关的已经隐藏,报错的就是上面一行)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCalla