Java easymock,mock返回一个mock

Java easymock,mock返回一个mock,java,easymock,Java,Easymock,我正在使用EasyMock测试我的Java代码 我要模拟的代码如下所示: requestInfo = mupClient.newEnqueueRequestCall().call(requestArgs); 我嘲笑这一点的方式是: expect(mupClient.newEnqueueRequestCall()).andReturn(enqueueRequestCall); final Capture<EnqueueRequestArgs> captureRequestArgs =

我正在使用EasyMock测试我的Java代码

我要模拟的代码如下所示:

requestInfo = mupClient.newEnqueueRequestCall().call(requestArgs);
我嘲笑这一点的方式是:

expect(mupClient.newEnqueueRequestCall()).andReturn(enqueueRequestCall);
final Capture<EnqueueRequestArgs> captureRequestArgs = 
                         new Capture<EnqueueRequestArgs>();
expect(mupClient.newEnqueueRequestCall().call(capture(captureRequestArgs))).
                         andThrow(new MUPCoralException("an exception"));
expect(mupClient.newEnqueueRequestCall()).andReturn(enqueueRequestCall);
最终捕获captureRequestArgs=
新捕获();
expect(mupClient.newEnqueueRequestCall().call(capture(captureRequestArgs)))。
Andhrow(新的MUPCoralException(“例外”));
但是
requestInfo
总是
null
。即使我将
.anthrow()
部分更改为
.andReturn(new RequestInfo())
,它仍然是
null

我查看了另一个类似的帖子,但没有成功。现在我能够对它进行评论,从而提出一个新问题

答复: 在
回放中添加所有模拟对象
!示例
重播(mockObj1,mockObj2,…)
尝试以下操作:

expect(mupClient.newEnqueueRequestCall()).andReturn(enqueueRequestCall);
final Capture<EnqueueRequestArgs> captureRequestArgs = 
                          new Capture<EnqueueRequestArgs>();
expect(enqueueRequestCall.call(capture(captureRequestArgs))).
                          andThrow(new MUPCoralException("an exception"));
expect(mupClient.newEnqueueRequestCall()).andReturn(enqueueRequestCall);
最终捕获captureRequestArgs=
新捕获();
expect(enqueueRequestCall.call(capture(captureRequestArgs)))。
Andhrow(新的MUPCoralException(“例外”));

问题是您的
enqueryquestcall
应该返回
requestInfo
mupClient
仅在您从easymock调用
replay
方法之后才会返回
enqueueRequestCall

我也试过了。我解决了这个问题。我没有在回放中添加模拟。成功了。例如,我有
replay(mock1)
,而它应该是
replay(mock1,mock2)
。非常感谢。