How to properly record mocks method when as an argument I put a result of another method of the same mock:
mockObj.doSth(arg1, arg2, mockObj.doSthElse(), arg2);
I'm testing a class method with class field as a mock (documentHelper):
OperationInfo operationInfo = documentHelper.validate(document, documentHelper.getValidationDate(opData, document, true), lang, false);
Now my method test looks like this:
@Test
public void getOperationData_CheckClass() {
//record
this.recordGetDocument();
DateTime dateTime = documentHelper.getValidationDate(operationData, document, true);
expectLastCall().andReturn(new DateTime()).times(1);
documentHelper.validate(document, dateTime, operation.getCustomPrincipal().getLang(), false);
expectLastCall().andReturn(new OperationInfo()).times(1);
//replay
replay(documentHelper);
//call
OperationData opdata = operation.getOperationData(id, operationCode, null);
Assert.assertEquals(operationData.getClass().getName(), opdata.getClass().getName());
//verify
verify(documentHelper);
}
And getting error like:
java.lang.AssertionError:
Unexpected method call getValidationDate(...
on operation.getOperationData method invoked
You need to think of it as:
int tmp = mockObj.doSthElse();
mockObj.doSth(arg1, arg2, tmp, arg3);
So you set up the expectation (and return value) for doSthElse
, then you expect that value as the argument for doSth
.
I can't explain why you're getting an unexpected method call for getValidationDate()
, but the dateTime
value you're passing into the validate
mock is incorrect - you should be using:
DateTime dateTime = new DateTime();
documentHelper.getValidationDate(operationData, document, true).andReturn(dateTime);
See more on this question at Stackoverflow