0
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());
所以,以下語句匹配內
return ((Event2) arg).getSth==sth;
會拋出這個exception.So的問題是如何讓
verify(mock).fireEvent(argThat(new Event2Matcher()));
匹配第二次調用?