easymock捕获抛出NullPoInterException
我突然偶然发现了一个问题,我需要捕获该界面的参数,称为:
public interface MyInterface {
void doSomething(long id);
}
@Mock
private MyInterface myInterface;
...
Capture<Long> capturedId = Capture.newInstance();
myInterface.doSomething(capture(capturedId));
测试很早失败:
java.lang.nullpointerexception 在com.mycompany.myinterfacetest.dosomethingtest(myinterfacetest.java:81) 在sun.reflect.nativemethodaccessorimpl.invoke0(天然方法) 在sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) 在sun.reflect.delegatingmethodaccessorimpl.invoke(授权methodaccessorimpl.java:43) 在java.lang.reflect.method.invoke(method.java:498) atorg.junit.runners.model.frameworkmethod $ 1.RunreFlectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.reflectivecallable.run(ReflectiveCallable.java:12)
我三次检查了,没有传递 null
,这是怎么回事?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是实施 始终返回
null
的方法:如果模拟方法接受对象类型
长作为参数:
...但是,存在
长
原始类型,并且尝试拆下 null )的尝试://easymock.org/api/org/asymock/easymock.html#capture-org.easymock.capture-“ rel =” nofollow noreferrer“>easymock#capture(capture&lt; t&gt; t&gt;)
< /a>方法在nullpointerexception
上失败。修复程序很简单:因此,引入了适用于原始类型的方法变化,例如
easymock#capturelong(capture&lt; long&gt;)
:The issue is the implementation of the
EasyMock#capture(Capture<T>)
method that always returnsnull
:This would not be a problem if the mocked method would accept an object type
Long
as a parameter:... however, the
long
primitive type is present and the attempt of unboxing the return value (null
) of theEasyMock#capture(Capture<T>)
method fails onNullPointerException
.The fix is simple: For this reason, there were introduced method variations suitable for primitive types, such as
EasyMock#captureLong(Capture<Long>)
: