Can I mix one matcher and 1 exact value in mockito arguments

Viewed 281

If I create a mock like this

when(servicesTestEnv.mockUserProfileAndPortfolioTransactionRepository.get(servicesTestEnv.mockDistributedTransaction,ArgumentMatchers.any[ExternalUserProfileKeys]))
        .thenReturn(Right(servicesTestEnv.externalUserProfile))

I get error

Invalid use of argument matchers! 2 matchers expected, 1 recorded: -> at UnitSpecs.ServiceSpecs.UserTransactionDatabaseServiceSpecs.$anonfun$new$17(UserTransactionDatabaseServiceSpecs.scala:170)

But if I replace both with value

when(servicesTestEnv.mockUserProfileAndPortfolioTransactionRepository.get(servicesTestEnv.mockDistributedTransaction,keys))
        .thenReturn(Right(servicesTestEnv.externalUserProfile))

or

when(servicesTestEnv.mockUserProfileAndPortfolioTransactionRepository.get(ArgumentMatchers.any[DistributedTransaction],ArgumentMatchers.any[ExternalUserProfileKeys]))
        .thenReturn(Right(servicesTestEnv.externalUserProfile))

then I don't get the error

Is this a rule in Mockito that all argument either need to matchers or values?

1 Answers

Mixing of matchers and raw values is not supported by Mockito at the moment and mocking has to be done fully one way (matchers) or the other (concrete values), just like you've shown in your question.

There's a discussion on Mockito GitHub page regarding mixed argument mocking, but it's been pretty much dead for two years.

Related