服务等级:
public void createManualEvaluationReductionChangeHistory(Long key, String accountId, RegisterReductionPerFunction registerReductionPerFunction, String languageCode, String comments, String pagRedFlag) {
    ProfessionalCustomerHistory professionalCustomerHistory = new ProfessionalCustomerHistory();
    professionalCustomerHistory.setDescription(comments);
    professionalCustomerHistory.setReductionCategory(registerReductionPerFunction.getReductionCategoryCode());
    professionalCustomerHistory.setReductionType(registerReductionPerFunction.getReductionTypeCode());
    professionalCustomerHistory.setValidityId(registerReductionPerFunction.getValidityId().longValue());
    professionalCustomerHistory.setReductionPercentage(reductionCategoryService.getReductionPercentage(languageCode,
            registerReductionPerFunction.getReductionCategoryCode(), registerReductionPerFunction.getReductionTypeCode()));
    professionalCustomerHistory.setTotalReduction(professionalCustomerHistory.getReductionPercentage());
    professionalCustomerHistory.setPagFixedReductionFlag(pagRedFlag);
    setCommonHistoryDetails(professionalCustomerHistory, Constants.NO, accountId, key, Constants.HISTORY_TYPE_REDUCTIONS);
    professionalCustomerHistoryDlService.create(professionalCustomerHistory);
}
Junit 测试:@Test
public void createManualEvaluationReductionChangeHistory() {
    ProfessionalCustomerHistory professionalCustomerHistory = new ProfessionalCustomerHistory();
    RegisterReductionPerFunction registerReductionPerFunction = new RegisterReductionPerFunction();
    professionalCustomerHistory.setValidityId(1L);
    registerReductionPerFunction.setValidityId(1);
    professionalCustomerHistory.setProfCustomerId(PROF_CUST_ID);
    professionalCustomerHistory.setHistoryType("RD");
    professionalCustomerHistory.setEditedBy(ACCOUNT_ID);
    professionalCustomerHistory.setHistoryDate(new Date());
    professionalCustomerHistory.setNoDeleteFlag("N");
    professionalCustomerHistory.setReductionPercentage(null);
    professionalCustomerHistory.setTotalReduction(null);
    professionalCustomerHistory.setDescription(COMMENTS);
    Mockito.when(reductionCategoryService.getReductionPercentage(LANGUAGE_CODE, null, null)).thenReturn(null);
    profCustomerHistoryService.createManualEvaluationReductionChangeHistory(PROF_CUST_ID, ACCOUNT_ID.toString(), registerReductionPerFunction, LANGUAGE_CODE, COMMENTS, null);
    Mockito.verify(reductionCategoryService).getReductionPercentage(LANGUAGE_CODE,null,null);
    Mockito.verify(professionalCustomerHistoryDlService).create(professionalCustomerHistory);
}
当我测试它时,它低于错误。
论据不同!想要:实际调用有不同的参数:
但我看到所有参数都完全相同。可能是什么导致了这个问题?
ProfessionalCustomerHistory 是一个数据库实体,我没有
equals并且hashcode
假设只有你的第二个verify失败了,这就是问题所在。
目前,您ProfessionalCustomerHistory在测试和逻辑中创建了不同的对象。它们可能具有相同的内容,但没有正确的实现equals和hashcode方法,java中的默认实现只关心对象引用。
如果您使用 IDE,它可能有一些生成方法,可以让您生成正确的equals方法hashCode。
如果您只想验证是否调用了正确的方法,而不关心确切的内容,您可以使用:
Mockito.verify(professionalCustomerHistoryDlService)
       .create(Mockito.any(ProfessionalCustomerHistory.class));
如果您不能或不想更改ProfessionalCustomerHistory类,您可以使用 anArgumentCaptor并随后比较不同的字段。
| 归档时间: | 
 | 
| 查看次数: | 10329 次 | 
| 最近记录: |