2

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.

ありがとうございました。

4

1 に答える 1

6

後でスパイするのではなく、スパイされたインスタンスをアクティビティに渡すべきではありませんか?

com.google.gwt.event.shared.testing.CountingEventBusメソッドを追加して、単純なa (ラップするインスタンスを渡さない限りEventBusnewを使用)を使用することもできることに注意してください。その後、SimpleEventBusEventBusgetCount(GwtEvent.Type)assertEquals(1, countingEventBus.getCount(AssetEvent.TYPE));

于 2010-12-16T11:42:28.963 に答える