mockito/mockito-scala

Unable to match part of the argument

gabrbuiv opened this issue · 2 comments

case class Bar(id: String, length: Double, slices: Array[(Long, Double)])) {
   ...
}

class Repository(){
   def foo(bars: Iterable[Bar]): Unit = {
      ...
   }
}

class MyTests extends AnyFunSuite with IdiomaticMockito with ArgumentMatchersSugar{
   val mocked = mock[Repository]
   val bars = Seq(Bar("string", 10.0, AdditionalMatchers.aryEq(Array.empty)))
   mocked.foo(bars) was called
}
Misplaced or misused argument matcher detected here:
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

I'm struggling to understand how to use the mix and match concept. Is it even possible to match arguments in the object that is passed to a method?

Hi,

You can't use argument matchers inside the arguments themselves, they are meant to replace arguments to the mocked method.

A way to achieve what you want could be using a partial function matcher, something along these lines

mocked.foo(argMatching({ case Seq(Bar("string", 10.0, _)) => })) was called

Thank you very much for a quick response! It worked!