Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito keeps returning Empty List

I'm working on unit testing a method in Mockito and mockito keeps sending an empty zero size list even when I have initialized the list that is to be returned.

This is the code to be tested. Note that nonCashIncludedPaymentPlanActive is always true ( Mocked ).

    List<DebtAccountTransaction> debtAccountTransactionList = null;

    boolean nonCashIncludedPaymentPlanActive = balancingPlanService.checkNonCashIncludedPaymentPlanParameter(debtAccountId);


    if (nonCashIncludedPaymentPlanActive) {
        debtAccountTransactionList = debtAccountTransactionDao
                .getDebtAccountTransactionListByDebtAccountIdListWithCN(baseDebtIdAccountList, null);
    } 
    if (debtAccountTransactionList.isEmpty()) {
        throw new SfcException("DISPLAY.PAYMENT_PLAN_WITH_NO_BALANCE_SERVICE_FILE_CLOSED");
    }

This the statement that keeps returning a List that I have mocked in mockito and added an item to it and here it returns an emptylist.

debtAccountTransactionList = debtAccountTransactionDao
                .getDebtAccountTransactionListByDebtAccountIdListWithCN(baseDebtIdAccountList, null);

which then ofcourse gets caught by this line

if (debtAccountTransactionList.isEmpty()) {
        throw new SfcException("DISPLAY.PAYMENT_PLAN_WITH_NO_BALANCE_SERVICE_FILE_CLOSED");
    }

Thus inorder to avoid this path of execution I have done the following in Mockito:

when(debtAccountTransactionDao.getDebtAccountTransactionListByDebtAccountIdListWithCN(baseDebtIdAccountList, null)).thenReturn(
            debtAccountTransactionList);

and declaration of debtAccountTransactionList is :

DebtAccountTransaction debtAccountTransaction = spy(DebtAccountTransaction.class);
    debtAccountTransaction.setId(2L);


    List<DebtAccountTransaction> debtAccountTransactionList = new ArrayList<DebtAccountTransaction>();
    debtAccountTransactionList.add(debtAccountTransaction);

I tried mocking a List, tried different argument captors but nothing seems to work. When I debug it, Mockito does fill up the debtAccountTransactionList but with an empty List, thus it fails.

Any help with how I can make sure that Mockito sends a Non-Empty Non-Zero List so that it can bypass the isEmpty() check.

like image 898
AbuBakar Khan Avatar asked Dec 13 '22 14:12

AbuBakar Khan


1 Answers

A good rule of thumb in writing tests, especially with a mocking library like Mockito: Don't confuse stubbing with verification. Stubbing (when) is about getting your system under test (SUT) into the desired state, not about asserting anything about how the SUT behaves.

In Mockito, the way to make assertions about how the SUT behaves is after the SUT runs, using verify calls. If you don't have any verify calls, you're not actually asserting anything, and your SUT can misbehave without your test catching it, which is obviously a bad thing.

As a result, it's usually best to make your matchers for stubbing (when) as broad as possible, since the goal of stubbing is just to make sure you fall into the right test case. For example, you can and often should use matchers like any() in your when() call. If you did that, you would sidestep the issue you're having here.

If you want to make assertions about the values that the SUT actually used as arguments, do that with verify, or possibly by capturing the value and making additional assertions about it directly.

like image 110
Daniel Pryden Avatar answered Dec 21 '22 11:12

Daniel Pryden