[英]easymock, mock returning a mock
I am testing my Java code using EasyMock. 我正在使用EasyMock测试我的Java代码。
The piece of code that I want to mock looks like this: 我要模拟的代码如下:
requestInfo = mupClient.newEnqueueRequestCall().call(requestArgs);
The way I am mocking this is: 我嘲笑的方式是:
expect(mupClient.newEnqueueRequestCall()).andReturn(enqueueRequestCall);
final Capture<EnqueueRequestArgs> captureRequestArgs =
new Capture<EnqueueRequestArgs>();
expect(mupClient.newEnqueueRequestCall().call(capture(captureRequestArgs))).
andThrow(new MUPCoralException("an exception"));
But requestInfo
is always null
. 但是
requestInfo
始终为null
。 Even if I change the .andThrow()
part to .andReturn(new RequestInfo())
, it is still null
. 即使将
.andThrow()
部分更改为.andReturn(new RequestInfo())
,它仍然为null
。
I checked the other similar post but that did not work. 我检查了另一个类似的职位,但这没有用。 Now was I able to comment on it and hence creating a new question.
现在我能够对此发表评论,从而提出了一个新问题。
ANSWER: add all mock'd objects in replay
! 解答:在
replay
添加所有模拟对象! Example replay(mockObj1, mockObj2, ...)
replay(mockObj1, mockObj2, ...)
示例replay(mockObj1, mockObj2, ...)
Try this: 尝试这个:
expect(mupClient.newEnqueueRequestCall()).andReturn(enqueueRequestCall);
final Capture<EnqueueRequestArgs> captureRequestArgs =
new Capture<EnqueueRequestArgs>();
expect(enqueueRequestCall.call(capture(captureRequestArgs))).
andThrow(new MUPCoralException("an exception"));
The problem is that your enqueRequestCall
should return requestInfo
. 问题是您的
enqueRequestCall
应该返回requestInfo
。 mupClient
will return enqueueRequestCall
only after you call replay
method from easymock. 仅当您从easymock调用
replay
方法后, mupClient
才会返回enqueueRequestCall
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.