简体   繁体   English

Mockito使用ArgumentCaptor抛出InvalidUseOfMatchersException

[英]Mockito throws InvalidUseOfMatchersException with ArgumentCaptor

I am trying to use mockito's ArgumentCaptor class to capture some parameter and then do some verifications on it. 我试图使用Mockito的ArgumentCaptor类捕获一些参数,然后对其进行一些验证。 But it's throwing an exception. 但这引发了异常。

This is what is printed as the error message. 这就是作为错误消息打印的内容。

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! org.mockito.exceptions.misusing.InvalidUseOfMatchersException:参数匹配器的无效使用! 0 matchers expected, 1 recorded: 预计有0个符合条件的记录,有1个记录:

Below is the code which is throwing the exception. 下面是引发异常的代码。

    //Arrange
    int amount = 100;

    DonationTransaction transaction = getPendingTransaction(player, amount);
    when(mockDonationTransactionDAO.getPendingTransactions(player)).thenReturn(Arrays.asList(transaction));

    ArgumentCaptor<DonationAttribution> argumentCaptor = ArgumentCaptor.forClass(DonationAttribution.class);

    //Act
    donationService.applyPendingDonations(player, playerDTO);

    //Assert
    verify(mockDonationAttributionDAO).save(argumentCaptor.capture()); //Exception here
    ...

I am using Junit5 and mockito version 2.7.22. 我正在使用Junit5和Mockito版本2.7.22。

Not sure if I am missing something obvious here. 不知道我是否在这里缺少明显的东西。

My DonationAttributionDao extends an abstract DAO if that helps with anything and the save method is defined in the abstract class which takes as parameter the base class of the Argument I am trying to capture. 我的DonationAttributionDao可以扩展抽象DAO(如果有帮助的话),并且save方法在抽象类中定义,该抽象类将我尝试捕获的Argument的基类作为参数。

The save() method is static or final. save()方法是静态的或最终的。 Thus, you cannot mock it. 因此,您不能嘲笑它。 You can try to use PowerMock instead (see Mockito - 0 Matchers Expected, 1 Recorded (InvalidUseOfMatchersException) ). 您可以尝试改用PowerMock(请参阅Mockito-预期0个匹配项,已记录1个(InvalidUseOfMatchersException) )。

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

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