简体   繁体   中英

Mocking chained method calls using mockito?

I need to mock this:

 void handleCellPreview(CellPreviewEvent<List<String>> event) {
    Element cellElement = event.getNativeEvent().getEventTarget().cast();
 }

I am doing this:

CellPreviewEvent<List<String>> cellPreviewEvent = Mockito.mock(
        CellPreviewEvent.class, Mockito.RETURNS_DEEP_STUBS);
Element cellElement = Mockito.mock(Element.class, Mockito.RETURNS_DEEP_STUBS);
EventTarget eventTarget = Mockito.mock(EventTarget.class);
  Mockito.when(cellPreviewEvent.getNativeEvent().getEventTarget().cast()).thenReturn(cellElement);


And I am getting following error:

testHandleCellPreview(client.view.MyViewTest)java.lang.NullPointerException
    at com.google.gwt.dom.client.NativeEvent.getEventTarget(NativeEvent.java:137)
    atclient.view.MyViewTest.testHandleCellPreview(MyViewTest.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


I've also seen, the same question below:
mock or stub for chained call

Can anybody please point out what I am missing?

Thanks,
Mohit

I think the issue is that you are attempting to execute GWT code outside of a client browser environment. GWT is designed to be converted to JavaScript and run on a browser. I am not sure it will work otherwise.

I noticed that line 137 of NativeEvent appears to be DomImpl.impl.eventGetTarget . This leads me to believe that DomImpl.impl is null .

I found the following by looking into the code:

45  public static <T> T create(Class<?> classLiteral) {
46     if (sGWTBridge == null) {
47       /*
48        * In Production Mode, the compiler directly replaces calls to this method
49        * with a new Object() type expression of the correct rebound type.
50        */
51       throw new UnsupportedOperationException(
52           "ERROR: GWT.create() is only usable in client code!  It cannot be called, "
53               + "for example, from server code.  If you are running a unit test, "
54               + "check that your test case extends GWTTestCase and that GWT.create() "
55               + "is not called from within an initializer or constructor.");
56     } else {
57       return sGWTBridge.<T> create(classLiteral);
58     }
59   }

Have you extended GWTTestCase

You need to set mocked objects again in parent entity. So that at run - time , it uses the mocked objects.

cellPreviewEvent.setCellElement(cellElement);
cellPreviewEvent.setEventTarget(eventTarget);

Complete code would look like:

CellPreviewEvent<List<String>> cellPreviewEvent = Mockito.mock(
        CellPreviewEvent.class, Mockito.RETURNS_DEEP_STUBS);
Element cellElement = Mockito.mock(Element.class, Mockito.RETURNS_DEEP_STUBS);
EventTarget eventTarget = Mockito.mock(EventTarget.class);
cellPreviewEvent.setCellElement(cellElement);
cellPreviewEvent.setEventTarget(eventTarget);
  Mockito.when(cellPreviewEvent.getNativeEvent().getEventTarget().cast()).thenReturn(cellElement);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM