用mockito嘲笑GWT EventBus [英] mocking GWT EventBus with mockito
问题描述
我在执行EventBus的SimpleEventBus实现时遇到了一些问题:
我有一个活动,它也是特定事件的处理程序。
代码:
@Mock私人AssetCellList视图;
@Mock private AcceptsOneWidget面板;
@Mock private SelectionModel< Asset> selectionModel设置;
@Mock private HasData< Asset> cellList;
@Mock私人AssetService服务;
@Mock私人资产资产;
@Mock私人列表< Asset>列表;
@Mock private AssetListDTOClientImpl assetDTO;
@Mock private AssetEvent事件;
@Before
public void setUp()抛出异常{
MockitoAnnotations.initMocks(this);
@Test
public void test(){
/ *一些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);
/ *创建真实EventBus * /
EventBus eventBus = new SimpleEventBus();
/ *创建活动* /
AssetListActivity activity = new AssetListActivity(eventBus,
view,
service);
/ *侦测eventBus * /
EventBus eventBusSpy = spy(eventBus);
/ *监视活动* /
AssetListActivity activitySpy = spy(activity);
/ *开始活动* /
activity.start(panel);
/ *验证服务电话 - > OK * /
verify(service,times(1))。getAssets(anyInt());
/ *模拟最终引发事件的服务* /
eventBus.fireEvent(event);
/ *验证eventBus确实触发了事件 - > NO OK * /
verify(eventBusSpy,times(1))。addHandler(eq(AssetEvent.TYPE),isA(AssetEventHandler.class));
/ *稍后验证* /
验证(activitySpy).onAssetsReceived(event);
错误追踪在eventBusSpy验证中说:
通缉但未调用:
simpleEventBus.addHandler(
事件类型,
isA(猫.ccma.testproject.client.events.AssetEventHandler)
);
- >在cat.ccma.testproject.client.AssetListTest.test(AssetListTest.java:87)
其实,这个模拟与零交互。
谢谢。
解决方案
请注意,您也可以使用 com .google.gwt.event.shared.testing.CountingEventBus
这是一个简单的 EventBus
(使用新的除了传递一个 EventBus
实例进行包装)以及添加一个 getCount(GwtEvent.Type)的SimpleEventBus
code>方法。
然后执行 assertEquals(1,countingEventBus.getCount(AssetEvent.TYPE));
I have some problems spying on a real SimpleEventBus implementation of EventBus:
I have an activity which is also a handler for a specific event. This event is fired by a service.
The code:
@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);
}
The error trace is in th eventBusSpy verification and says:
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.
Thank You.
解决方案 Shouldn't you pass the spied instance to your activity, instead of spying it afterwards?
Note you can also use a com.google.gwt.event.shared.testing.CountingEventBus
which is a simple EventBus
(uses new SimpleEventBus
unless you pass an EventBus
instance to be wrapped) with the addition of a getCount(GwtEvent.Type)
method.
You'd then do an assertEquals(1, countingEventBus.getCount(AssetEvent.TYPE));
这篇关于用mockito嘲笑GWT EventBus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文