简体   繁体   English

Mockito不断返回空列表

[英]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. 我正在对Mockito中的方法进行单元测试,即使我已经初始化了要返回的列表,mockito仍会发送一个空的零大小列表。

This is the code to be tested. 这是要测试的代码。 Note that nonCashIncludedPaymentPlanActive is always true ( Mocked ). 请注意,nonCashIncludedPaymentPlanActive始终为true(模拟)。

    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: 因此,为了避免执行此路径,我在Mockito中做了以下操作:

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

and declaration of debtAccountTransactionList is : btcAccountTransactionList的声明为:

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. 当我调试它时,Mockito确实填满了bondAccountTransactionList,但列表为空,因此它失败了。

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. 关于如何确保Mockito发送非空非零列表的任何帮助,以便它可以绕过isEmpty()检查。

A good rule of thumb in writing tests, especially with a mocking library like Mockito: Don't confuse stubbing with verification. 编写测试的良好经验法则,尤其是使用Mockito之类的模拟库时:不要将存根与验证混为一谈。 Stubbing ( when ) is about getting your system under test (SUT) into the desired state, not about asserting anything about how the SUT behaves. 存根( when )是要使您的被测系统(SUT)进入所需状态, 而不是 断言有关SUT行为的任何内容。

In Mockito, the way to make assertions about how the SUT behaves is after the SUT runs, using verify calls. 在Mockito中,对SUT的行为进行断言的方法是 SUT运行后使用verify调用。 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. 如果您没有任何verify调用,那么您实际上并没有声明任何内容,并且如果您的测试未捕获到您的SUT,它可能会发生异常行为,这显然是一件坏事。

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. 因此,通常最好使您的匹配器尽可能地广泛地进行存根( when ),因为存根的目的只是确保您进入正确的测试用例。 For example, you can and often should use matchers like any() in your when() call. 例如,您可以并且经常应在when()调用中使用像any()这样的匹配器。 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. 如果要对SUT实际用作参数的值进行断言,请使用verify或通过捕获值并直接对其进行其他断言来进行。

Did you put in any place of the code something like: 您是否将代码放在任何地方,例如:

debtAccountTransactionDao = Mockito.mock(NameOfTheClassOfThisDebtAccountObject.class);

?

You should put something like this before calling the method getDebtAccountTransactionListByDebtAccountIdListWithCN , so it knows it should use the mocked behavior, not the normal behavior of the method (that could be return an empty list). 您应该在调用方法getDebtAccountTransactionListByDebtAccountIdListWithCN之前放置类似的getDebtAccountTransactionListByDebtAccountIdListWithCN ,这样它知道它应该使用getDebtAccountTransactionListByDebtAccountIdListWithCN行为,而不是方法的常规行为(可能会返回一个空列表)。

The problem is the mock creation/behavior registration. 问题是模拟创建/行为注册。 This doesn't match what you put into the method and hence returns to the default behavior of returning an empty list. 这与您在方法中输入的内容不匹配,因此返回默认的返回空列表的行为。

as pointed out by M. Deinum 正如Deinum先生所指出的

Thus there was a problem in acceptance of the argument by Mockito and it would ignore my stubbing and then return an empty list by default. 因此,Mockito接受参数存在问题,它将忽略我的存根,然后默认情况下返回一个空列表。

I fixed it by making sure the object that is baseDebtIdAccountList passed to the function when(debtAccountTransactionDao.getDebtAccountTransactionListByDebtAccountIdListWithCN(baseDebtIdAccountList, null)).thenReturn( debtAccountTransactionList) is exactly the same in the rest of the code. 我通过确保在(debtAccountTransactionDao.getDebtAccountTransactionListByDebtAccountIdListListCNCN(baseDebtIdAccountList,null))。thenReturn(btcAccountTransactionList)传递给函数的情况下确保baseDebtIdAccountList对象传递给函数来进行修复 Thus there was a mismatch in the arguments and Mockito used the default way of using an empty list. 因此,参数不匹配,Mockito使用了使用空列表的默认方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM