junitmockito

Argument(s) are different! Wanted: Actual invocation has different arguments:


ServiceClass:

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

When i am testing it getting below error.

Argument(s) are different! Wanted: Actual invocation has different arguments:

But i see all the parameters are exact the same. what might be causing the issue?


Solution

  • ProfessionalCustomerHistory is a DB entity, i dont have equals and hashcode

    Assuming that its only your 2nd verify that fails, this is the problem.

    Currently you create a different ProfessionalCustomerHistory object in your test and in your logic. They might have the same content but without properly implemented equals and hashcode methods, the default implementation in java only cares about the object reference.

    If you use an IDE it probably has some generate method that lets you generate the proper equals and hashCode methods.


    If you only want to validate that the correct method is called, without caring about the exact content, you could use:

    Mockito.verify(professionalCustomerHistoryDlService)
           .create(Mockito.any(ProfessionalCustomerHistory.class));
    

    If you can not or do not want to change the ProfessionalCustomerHistory class, you could use an ArgumentCaptor and compare the distinct fields afterwards.