How to let the matcher to match the second invocation on mock?
- by Alex Luya
I have an interface like this
public interface EventBus{
public void fireEvent(GwtEvent<?> event);
}
and test code(testng method) looks like this:
@Test
public void testFireEvent(){
EventBus mock=mock(EventBus.class);
//when both Event1 and Event2 are subclasses of GwtEvent<?>
mock.fireEvent(new Event1());
mock.fireEvent(new Event2());
//then
verify(mock).fireEvent(argThat(new Event2Matcher()));
}
Event2Matcher looks like this:
private class Event2Matcher extends ArgumentMatcher<Event2>
{
@Override
public boolean matches(Object arg)
{
return ((Event2) arg).getSth==sth;
}
}
But get an error indicating that:
Event1 can't be cast to Event2
And obviously,the matcher matched the first invoking
mock.fireEvent(new Event1());
So,the statement within matcher
return ((Event2) arg).getSth==sth;
Will throw out this exception.So the question is how to let
verify(mock).fireEvent(argThat(new Event2Matcher()));
to match the second invoking?