2

我需要嘲笑这个:

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

我正在这样做:

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);


我收到以下错误:

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)


我也看到了,下面同样的问题:
mock or stub for chained call

有人可以指出我缺少什么吗?

谢谢,
莫希特

4

2 回答 2

1

我认为问题在于您试图在客户端浏览器环境之外执行 GWT 代码。GWT 旨在转换为 JavaScript 并在浏览器上运行。我不确定它是否会起作用。

我注意到第 137 行NativeEvent似乎是DomImpl.impl.eventGetTarget. 这让我相信那DomImpl.implnull

我通过查看代码发现了以下内容:

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   }

你有没有延长GWTTestCase

于 2012-10-22T11:32:48.577 回答
-1

您需要在父实体中再次设置模拟对象。所以在运行时,它使用模拟对象。

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

完整的代码如下所示:

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);
于 2012-10-22T09:01:01.330 回答