How to let the matcher to match the second invocation on mock?

Posted by Alex Luya on Stack Overflow See other posts from Stack Overflow or by Alex Luya
Published on 2013-07-02T14:30:44Z Indexed on 2013/07/02 17:05 UTC
Read the original article Hit count: 254

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about mocking

Related posts about testng