easymock捕获抛出NullPoInterException

发布于 2025-01-24 04:45:37 字数 966 浏览 0 评论 0 原文

我突然偶然发现了一个问题,我需要捕获该界面的参数,称为:

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 ,这是怎么回事?

I suddenly stumbled upon an issue that I need to capture a parameter of this interface called:

public interface MyInterface {

    void doSomething(long id);
}
@Mock
private MyInterface myInterface;

...

Capture<Long> capturedId = Capture.newInstance();
myInterface.doSomething(capture(capturedId));

The test failed quite early:

java.lang.NullPointerException
  at com.mycompany.MyInterfaceTest.doSomethingTest(MyInterfaceTest.java:81)
  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:47)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

I triple-checked that no way a null is passed, what is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十二 2025-01-31 04:45:37

问题是实施 始终返回 null 的方法:

public static <T> T capture(final Capture<T> captured) {
    reportMatcher(new Captures<T>(captured));
    return null;
}

如果模拟方法接受对象类型作为参数:

void doSomething(long id);

...但是,存在原始类型,并且尝试拆下 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;)

myInterface.doSomething(captureLong(capturedId));

The issue is the implementation of the EasyMock#capture(Capture<T>) method that always returns null:

public static <T> T capture(final Capture<T> captured) {
    reportMatcher(new Captures<T>(captured));
    return null;
}

This would not be a problem if the mocked method would accept an object type Long as a parameter:

void doSomething(long id);

... however, the long primitive type is present and the attempt of unboxing the return value (null) of the EasyMock#capture(Capture<T>) method fails on NullPointerException.

The fix is simple: For this reason, there were introduced method variations suitable for primitive types, such as EasyMock#captureLong(Capture<Long>):

myInterface.doSomething(captureLong(capturedId));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文