Easymock使用另一个方法调用作为参数来模拟对象方法调用

时间:2014-06-04 07:51:10

标签: java testing easymock

如何在作为参数时正确记录mocks方法我将同一个mock的另一个方法的结果放在一起:

mockObj.doSth(arg1, arg2, mockObj.doSthElse(), arg2);

我正在使用类字段作为mock(documentHelper)测试类方法:

  OperationInfo operationInfo = documentHelper.validate(document, documentHelper.getValidationDate(opData, document, true), lang, false);

现在我的方法测试看起来像这样:

 @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);
    }

得到错误:

java.lang.AssertionError: 
  Unexpected method call getValidationDate(...
关于调用operation.getOperationData方法的

1 个答案:

答案 0 :(得分:0)

您需要将其视为:

int tmp = mockObj.doSthElse();
mockObj.doSth(arg1, arg2, tmp, arg3);

因此,您为doSthElse设置了期望值(和返回值),然后您希望该值作为doSth的参数。

我无法解释为什么您为getValidationDate()获得了意外的方法调用,但dateTime值已传入validate模拟不正确 - 您应该使用:

DateTime dateTime = new DateTime();
documentHelper.getValidationDate(operationData, document, true).andReturn(dateTime);