首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用mockito模拟GWT EventBus

用mockito模拟GWT EventBus
EN

Stack Overflow用户
提问于 2010-12-14 00:42:03
回答 1查看 3.9K关注 0票数 2

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

代码:

代码语言:javascript
复制
    @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验证中进行的,并表示:

代码语言:javascript
复制
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.

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-16 19:42:29

你不应该将被监视的实例传递给你的活动,而不是事后去监视它吗?

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4431131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档