Using Mockito's Matchers/ArgumentMatchers with custom ArgumentMatcher (to match POJO arguments)?

612 views
Skip to first unread message

Christian

unread,
May 29, 2018, 7:30:00 AM5/29/18
to mockito
Hello!

Given an object like the following:
public class Stuff {
  public Result doStuff(Pojo pojo) {
    // ...
  }
}
with 
public class Pojo {
  String bar;
  Integer baz;
  // getters, setters
}
is it possible to write a custom ArgumentMatcher for Pojo, so that you can write
when(mock.doStuff(argThat(new MyMatcher(eq("myBarValue"), anyInteger())))).thenReturn(/* ... */); 
using Mockito's matchers to perform "fuzzy" matching (e.g. any Integer will do this time)?

In other words, is it possible to write a custom ArgumentMatcher that supports Mockito's Matchers/ArgumentMatchers - in particular any() (and eq())?

Kind regards,
Christian
P.S.: Adding an equals method to Pojo is unfortunately not possible in my case.

Christian

unread,
Jun 1, 2018, 5:43:00 AM6/1/18
to mockito
Reply all
Reply to author
Forward
0 new messages