Java GwtMockito-单击多个按钮的处理程序

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()).

我对GwtMock和click处理程序有问题

在我的单元测试中,我有一个带有ClickHandler和Button的字段:

@格特莫克 私有ClickHandler ClickHandler

我的安装方法如下所示:

@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
    两次
  • 问题是,当我运行单元测试时,我在按钮“ChangeStatus”上创建了一个单击事件,但我想在按钮“Clear”上创建一个单击事件

    有趣的是,当我更改声明处理程序的顺序时,我可以通过“清除”按钮调用代码

    是的,这正是给定代码的预期行为。如果要区分按钮,请使用不同的模拟按钮


    在海事组织,更好的办法是: *使
    getClearButton
    getChangeStatusButton
    返回
    HasClickHandlers
    ,这样您甚至不需要GwtMockito,只需使用裸mokito即可。 *重构代码,以便视图添加单击处理程序,将演示者传递给视图,从而视图可以从单击处理程序调用演示者方法(例如
    presenter.clear()
    presenter.changeStatus()
    )。因此,对于presenter测试,您可以调用presenter方法。同样,您不再需要gwtMockito,只需使用裸Mockito即可。看


    在AICT中,GwtMockito更适合于不在代码中分离视图和演示者的情况,而是使用UiBinder,Java类充当演示者的角色,
    .ui.xml
    作为视图。

    使用GwtMock,您只能为按钮添加一个模拟。