unit-testing - 使用mockito模拟GWT EventBus

标签 unit-testing gwt mockito gwt-mvp

我在监视 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.

谢谢。

最佳答案

您不应该将监视的实例传递给您的事件,而不是事后监视它吗?

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

关于unit-testing - 使用mockito模拟GWT EventBus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4431131/

相关文章:

java - Mockito 相当于这个 Hamcrest "samePropertyValuesAs"/jMock "with"成语?

angularjs - 带指令的元素在单元测试中无法正确 $compile

javascript - Aurelia 自定义元素测试无法加载 View

visual-studio - UI 和事件测试

java - 将 html+css+js 转换为 PDF

javascript - Script# 的成熟度(与 GWT 相比)

java - 通过 GWT RPC 传递类对象的问题

java - 在使用 Mockito 测试时监视方法参数?

java - JUnit中的Matchers.refEq()比较对象的引用

java - Mockito 和 Hamcrest : how to verify invocation of Collection argument?