Combining fixed arguments with variable arguments in Mockito with Scala

When you use Mockito to mock an object and add behavior, you sometimes need to combine variable arguments with fixed arguments. When you do that in this way

when(client.execute("myId1", any[String])

You get as error Invalid use of argument matchers with the explanation that you should use eq as a matcher for the fixed argument.

However, eq defines referential equality in Scala, so you should use ArgumentMatchers.eq instead or use e.g. import org.mockito.ArgumentMatchers.{eq => eqTo} and use eqTo instead of eq. So the example above would become

import org.mockito.ArgumentMatchers.{eq => eqTo}

when(client.execute(eqTo("myId1"), any[String])

Leave a Reply

Your email address will not be published.