尽管在 .andReturn 中设置了新对象,EasyMock Expect 方法始终返回 null

发布于 2024-10-10 09:30:46 字数 585 浏览 3 评论 0原文

我的源代码为:

response = getRequestService().retrieveResponse(baseRequest);  
data = response.getStoredData(); ---> (1)

我的测试用例编写为:

RequestService requestService = EasyMock.createNiceMock(RequestService .class);  
BaseRequest baseRequest = new BaseRequest();  
BaseResponse response = new BaseResponse();  
expect(requestService .retrieveResponse(EasyMock.eq(baseRequest))).andReturn(response)
replay(requestService );  

但是,当我运行测试时,我总是在第 1 行收到 NullPointerException

有人可以帮我解决这个问题吗?

谢谢。

I have my source code as:

response = getRequestService().retrieveResponse(baseRequest);  
data = response.getStoredData(); ---> (1)

And I have written my Test Case as :

RequestService requestService = EasyMock.createNiceMock(RequestService .class);  
BaseRequest baseRequest = new BaseRequest();  
BaseResponse response = new BaseResponse();  
expect(requestService .retrieveResponse(EasyMock.eq(baseRequest))).andReturn(response)
replay(requestService );  

However, when I run my test I am always getting a NullPointerException at line 1.

Can somebody please help me on how to resolve this?

Thanks.

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

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

发布评论

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

评论(1

指尖上的星空 2024-10-17 09:30:46

我首先要检查的地方是确保 getRequestService() 调用返回对模拟请求服务的引用。您似乎尚未定义该行为,并且对 getRequestService() 的调用返回 null

The first place I would look would be to make sure the getRequestService() call returns a reference to your mock request service. It looks like you haven't yet defined that behavior, and the call to getRequestService() is returning null.

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