Mockito ArgumentMatcher saying Arguments are Different
- Try to simplify the test / code under test. Do you really need to mutate arguments and also verify stubbing? Both of those actions
- might indicate a code smell. Relax the argument verification and
- use some kind of any() matcher Perform validation of arguments in the
Like you said, it fails because the arguments are different. Take a look at the test below and you'll see that the second test method will fail because the status in your MyClass
instance is different from SomeStatus
that you passed in the matcher.
public class MatcherTest {
class MyClass{
private String status;
MyClass(String status) {
this.status = status;
}
public String getStatus(){
return status;
}
}
class MyDao {
public void update(MyClass myClass){}
}
class StatusMatcher extends ArgumentMatcher<MyClass> {
private String status;
public StatusMatcher(String hs) {
status = hs;
}
@Override
public boolean matches(Object argument) {
return status.equals(((MyClass)argument).getStatus());
}
}
@Test
public void shouldMatchStatus(){
MyDao mock = mock(MyDao.class);
mock.update(new MyClass("expectedStatus"));
verify(mock, times(1)).update(argThat(new StatusMatcher("expectedStatus")));
}
@Test
public void shouldNotMatchStatus(){
MyDao mock = mock(MyDao.class);
mock.update(new MyClass("unexpectedStatus"));
/* THE BELLOW WILL FAIL BECAUSE ARGUMENTS ARE DIFFERENT */
verify(mock, times(1)).update(argThat(new StatusMatcher("expectedStatus")));
}
}
I could take a wild guess that you could be reusing variables, or have a static field, etc. But without seeing your test code, no one can tell.