我有这样的界面
public interface EventBus{
public void fireEvent(GwtEvent<?> event);
}
和测试代码(testng 方法)看起来像这样:
@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 看起来像这样:
private class Event2Matcher extends ArgumentMatcher<Event2>
{
@Override
public boolean matches(Object arg)
{
return ((Event2) arg).getSth==sth;
}
}
但是得到一个错误提示:
Event1 can't be cast to Event2
显然,匹配器匹配了第一个调用
mock.fireEvent(new Event1());
所以,matcher中的语句
return ((Event2) arg).getSth==sth;
会抛出这个异常。所以问题是如何让
verify(mock).fireEvent(argThat(new Event2Matcher()));
匹配第二次调用?