簡體   English   中英

Easymock模擬對象方法調用,並以另一個方法調用作為參數

[英]Easymock mocks object method invocation with another method invocation as an argument

當我將相同模擬的另一個方法的結果作為參數時,如何正確記錄模擬方法:

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

我正在測試一個帶有類字段作為模擬的類方法(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方法的調用

您需要將其視為:

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

因此,您設置了doSthElse的期望值(和返回值),然后期望該值作為doSth的參數。

我無法解釋為什么為什么要對getValidationDate()進行意外的方法調用,但是要傳遞給validate模擬的dateTime值不正確-您應該使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM