简体   繁体   中英

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. 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! 0 matchers expected, 1 recorded:

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.

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.

The save() method is static or final. Thus, you cannot mock it. You can try to use PowerMock instead (see Mockito - 0 Matchers Expected, 1 Recorded (InvalidUseOfMatchersException) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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