GWT 和 MVP 模式中的 Mock 视图
我不知道问题是否已经提出,但我找不到它......我正在寻找一种方法来模拟我的观点以测试我的演示者?我尝试使用mockito作为视图,并将其设置在演示器中,但是在演示器中,当我调用presenter.getDisplay()(视图的getter)时,我的所有小部件都是null?因为我相信这是正常的,mockito 不会模拟小部件。
我百分百确定我弄错了什么,但我找不到它。
感谢您的启发:)
i dunno if the question is already ask, but i couldn't find it... i'm searching a way to mock my view in order to test my presenter ? i try to use mockito for the view, and set it in the presenter, but in result in presenter, when i call presenter.getDisplay() (the getter for the view) all of my widget is null ? as i believe it's normal mockito will not mock the widget.
i'm 100% sure i mistaken something but i couldnt find it.
thanks for your enlightement :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个非常简单的工作示例:
当然,通常您的 Presenter 不会位于您的测试用例内。
Here's a very simple working example:
Of course normally your Presenter wouldn't be inside your test case.
MVP Presenter 通过 gwt 接口依赖于 View 类 (
Display
),例如HasValue
、HasHTML
、HasClickHandlers
、等等,并可能根据需要添加新的接口。 Presenter 类应该使用这些接口,而不是直接使用 widget 类。因此,模拟视图接口相当简单,应该在测试方法之间共享(使用setUp
或@Before
)。这还应该包括模拟 GWT 基础设施,例如EventBus
等。有关使用 EasyMock 的示例的精彩博客(简单但不能直接转换为 mockito),请参阅 此处。
With MVP Presenter depends on View class (
Display
) via gwt interfaces, such asHasValue
,HasHTML
,HasClickHandlers
, etc. and possibly new interfaces as necessary. Presenter classes should use these interfaces instead of widget classes directly. Therefore Mocking View interfaces is rather simple and should be shared across test methods (usingsetUp
or@Before
). This should also include mocking GWT infrastructure such asEventBus
, etc.For nice blog with examples with EasyMock (easy but not straight forward to convert to mockito) see here.
您需要确保在调用 getDislay() 时告诉mockito 返回模拟视图。
某事喜欢
when(presenter.getDisplay()).thenReturn(mockView);
you need to make sure that you told mockito to return the mocked view when you call getDislay().
Sth like
when(presenter.getDisplay()).thenReturn(mockView);