简体   繁体   中英

easymock, mock returning a mock

I am testing my Java code using EasyMock.

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 . Even if I change the .andThrow() part to .andReturn(new RequestInfo()) , it is still 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 ! Example 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 . mupClient will return enqueueRequestCall only after you call replay method from easymock.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM