【问题标题】:mocking GWT EventBus with mockito用 mockito 模拟 GWT EventBus
【发布时间】:2010-12-13 16:42:02
【问题描述】:

我在监视 EventBus 的真正 SimpleEventBus 实现时遇到了一些问题: 我有一个活动,它也是特定事件的处理程序。此事件由服务触发。

代码:

    @Mock private AssetCellList view;
    @Mock private AcceptsOneWidget panel;
    @Mock private SelectionModel<Asset> selectionModel;
    @Mock private HasData<Asset> cellList;
    @Mock private AssetService service;
    @Mock private Asset asset;
    @Mock private List<Asset> list;
    @Mock private AssetListDTOClientImpl assetDTO;
    @Mock private AssetEvent event;


    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test 
    public void test(){


        /*Some stubbing*/
        when(view.getSelectionModel()).thenReturn(selectionModel);
        when(view.getList()).thenReturn(cellList);
        when(assetDTO.getAssetList()).thenReturn(list);
        when(assetDTO.getAssetList().get(anyInt())).thenReturn(asset);
        when(event.getAssetDTO()).thenReturn(assetDTO);


        /*Creatin the Real EventBus*/
        EventBus eventBus = new SimpleEventBus();

        /*Creating the activity */
        AssetListActivity activity = new AssetListActivity(eventBus, 
                view,
                service);

        /*Spying the eventBus*/
        EventBus eventBusSpy = spy(eventBus);
        /*Spying the activity*/
        AssetListActivity activitySpy = spy(activity);


        /*Starting the activity*/
        activity.start(panel);

        /*verifying the service call -> OK */
        verify(service, times(1)).getAssets(anyInt());

        /*Simulating the service which eventually fires an event*/
        eventBus.fireEvent(event);

        /*verifying that the eventBus really fires the event --> NO OK*/
        verify(eventBusSpy, times(1)).addHandler( eq( AssetEvent.TYPE ),                      isA(AssetEventHandler.class));

        /*later verifiction*/
        verify(activitySpy).onAssetsReceived(event);

    }

错误跟踪在 eventBusSpy 验证中并显示:

Wanted but not invoked:
simpleEventBus.addHandler(
    Event type,
    isA(cat.ccma.testproject.client.events.AssetEventHandler)
);
-> at cat.ccma.testproject.client.AssetListTest.test(AssetListTest.java:87)
Actually, there were zero interactions with this mock.

谢谢。

【问题讨论】:

    标签: unit-testing gwt mockito gwt-mvp


    【解决方案1】:

    您不应该将间谍实例传递给您的活动,而不是事后进行间谍活动吗?

    注意,您还可以使用com.google.gwt.event.shared.testing.CountingEventBus,它是一个简单的EventBus(使用新的SimpleEventBus,除非您传递要包装的EventBus 实例)并添加getCount(GwtEvent.Type) 方法。 然后你会做一个assertEquals(1, countingEventBus.getCount(AssetEvent.TYPE));

    【讨论】:

    • 谢谢托马斯。正如你所指出的,我没有通过间谍版的巴士。我还利用了 CountingEventBus。后面的验证呢。我可以在 jre 中使用这个 EventBus(火和事件)还是需要使用 GwtTest?
    • “共享”子包中的所有内容都可以在“纯 Java”和“GWT Java”代码中使用。这包括 SimpleEventBus 和 CountingEventBus。
    • 好的,也许我需要阅读更多关于对触发和事件等异步操作进行测试的信息,因为我无法从我的活动中接收到它(无法通过以后的验证)
    猜你喜欢
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2020-11-29
    • 2014-06-16
    相关资源
    最近更新 更多