Аргумент (ы) разные! Требуется: фактический вызов имеет разные аргументы: - PullRequest
0 голосов
/ 05 марта 2020

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

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

Когда я тестирую его, получаю ошибку ниже.

Аргумент (ы) разные! Требуется: фактический вызов имеет разные аргументы:

Но я вижу, что все параметры одинаковы. что может быть причиной проблемы?

1 Ответ

1 голос
/ 06 марта 2020

ProfessionalCustomerHistory является объектом БД, у меня нет equals и hashcode

Если предположить, что только ваш второй verify не работает, это проблема.

В настоящее время вы создаете другой объект ProfessionalCustomerHistory в своем тесте и в своей логике c. Они могут иметь одинаковое содержимое, но без правильно реализованных методов equals и hashcode, реализация по умолчанию в java касается только ссылки на объект.

Если вы используете IDE, у нее, вероятно, есть какой-то метод generate это позволяет вам генерировать правильные методы equals и hashCode.


Если вы хотите только проверить, что вызывается правильный метод, не обращая внимания на точное содержимое, вы можете использовать:

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

Если вы не можете или не хотите изменять класс ProfessionalCustomerHistory, вы можете использовать ArgumentCaptor и впоследствии сравнивать различные поля.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...