Java GwtMockito-单击多个按钮的处理程序
我对GwtMock和click处理程序有问题 在我的单元测试中,我有一个带有ClickHandler和Button的字段: @格特莫克 私有ClickHandler ClickHandler 我的安装方法如下所示:Java GwtMockito-单击多个按钮的处理程序,java,gwt,gwtmockito,Java,Gwt,Gwtmockito,我对GwtMock和click处理程序有问题 在我的单元测试中,我有一个带有ClickHandler和Button的字段: @格特莫克 私有ClickHandler ClickHandler 我的安装方法如下所示: @Before public void setUp() { when(this.display.getClearButton()).thenReturn(this.button); when(this.display.getChangeStatusButton()).
@Before
public void setUp() {
when(this.display.getClearButton()).thenReturn(this.button);
when(this.display.getChangeStatusButton()).thenReturn(this.button);
}
@Test
public void shouldClearFilterAfterClickClearFilterButton() {
// given
when(this.button.addClickHandler(any(ClickHandler.class))).thenAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock aInvocation) throws Throwable {
clickHandler = (ClickHandler) aInvocation.getArguments()[0];
return null;
}
});
this.presenter = new PresenterImpl(this.display, this.messages);
// when
clickHandler.onClick(clickEvent);
// then
this.presenter.asWidget();
}
我的测试结果如下:
@Before
public void setUp() {
when(this.display.getClearButton()).thenReturn(this.button);
when(this.display.getChangeStatusButton()).thenReturn(this.button);
}
@Test
public void shouldClearFilterAfterClickClearFilterButton() {
// given
when(this.button.addClickHandler(any(ClickHandler.class))).thenAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock aInvocation) throws Throwable {
clickHandler = (ClickHandler) aInvocation.getArguments()[0];
return null;
}
});
this.presenter = new PresenterImpl(this.display, this.messages);
// when
clickHandler.onClick(clickEvent);
// then
this.presenter.asWidget();
}
问题是,当我运行单元测试时,我在按钮“ChangeStatus”上创建了一个单击事件,但我想在按钮“Clear”上创建一个单击事件
有趣的是,当我更改声明处理程序的顺序时,我可以通过“清除”按钮调用代码
如何解决这个问题?如何在特定按钮上调用单击事件?让我们一起阅读代码:
getClearButton()
或getChangeStatusButton()
时,返回this.button
;也就是说,两个方法调用的按钮都是相同的,这意味着您将无法分辨哪个是哪个:它只是相同的addClickHandler
时,将单击处理程序存储在字段中;也就是说,如果调用了两次addClickHandler
,第二次调用将用第二个单击处理程序覆盖字段,并且您将不再引用第一个getClearButton()
和getChangeStatusButton()
并同时调用addClickHandler
;也就是说,在这个按钮上调用addClickHandler
两次
在海事组织,更好的办法是: *使
getClearButton
和getChangeStatusButton
返回HasClickHandlers
,这样您甚至不需要GwtMockito,只需使用裸mokito即可。
*重构代码,以便视图添加单击处理程序,将演示者传递给视图,从而视图可以从单击处理程序调用演示者方法(例如presenter.clear()
和presenter.changeStatus()
)。因此,对于presenter测试,您可以调用presenter方法。同样,您不再需要gwtMockito,只需使用裸Mockito即可。看
在AICT中,GwtMockito更适合于不在代码中分离视图和演示者的情况,而是使用UiBinder,Java类充当演示者的角色,
.ui.xml
作为视图。使用GwtMock,您只能为按钮添加一个模拟。