spöttischen GWT EventBus mit Mockito
-
09-10-2019 - |
Frage
Ich habe einige Probleme Spionage auf einem echten SimpleEventBus Implementierung von EventBus: Ich habe eine Aktivität, die für ein bestimmtes Ereignis auch ein Handler ist. Dieses Ereignis wird von einem Dienst entlassen.
Der 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);
}
Die Fehlertrace ist in th eventBusSpy Überprüfung und sagt:
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.
Danke.
Lösung
Sie sollten nicht die ausspioniert Instanz Ihre Aktivität übergeben, anstatt sie hinterher spioniert?
Beachten Sie können auch ein com.google.gwt.event.shared.testing.CountingEventBus
, die eine einfache EventBus
(verwendet neue SimpleEventBus
, wenn Sie eine EventBus
Instanz zu verpackenden passieren) ist mit dem Zusatz einer getCount(GwtEvent.Type)
Methode.
Sie würden dann eine assertEquals(1, countingEventBus.getCount(AssetEvent.TYPE));
tun