[英]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代码。 GWT is designed to be converted to JavaScript and run on a browser.
GWT旨在转换为JavaScript并在浏览器上运行。 I am not sure it will work otherwise.
我不确定它是否可以正常工作。
I noticed that line 137 of NativeEvent
appears to be DomImpl.impl.eventGetTarget
. 我注意到
NativeEvent
第137 NativeEvent
似乎是DomImpl.impl.eventGetTarget
。 This leads me to believe that DomImpl.impl
is null
. 这使我相信
DomImpl.impl
为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
您是否扩展了
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.