如何使用 JUnit 和 EasyMock 在 MVC 集成测试中模拟视图
我想模拟 MVC 设计模式的视图实现。我已经实现了 MVP
(另一个 MVC
变体),并且想测试当状态发生变化时控制器是否正确调用视图中的某些方法。模型。下面显示了模型
、控制器
和视图
上的方法调用顺序。
型号:
model.setProperty("newProperty");
控制器:
@Override public void propertyChange(PropertyChangeEvent evt) { for (View view : views) { view.modelPropertyChange(evt); } }
视图: 视图被调用的结果如下:
@Override public void modelPropertyChange(PropertyChangeEvent evt) { if ("Property".equals(evt.getPropertyName())) { updateView(); } }
问题:如何验证(在 JUnit
测试中使用 EasyMock
)、方法的预期顺序(带有有效参数) ) 执行?我期望 view.modelPropertyChange(evt)
被调用,并且期望 view.isViewUpdated()
在 view< 上返回
true
/代码> 对象。在我的 JUnit 测试中我该怎么说?请帮忙!
I would like to mock a view implementation of the MVC design pattern
. I have implemented the MVP
(another MVC
variation), and would like to test if the certain methods in the view get called correctly by the controller when a state change happens on the model. The following shows the sequence of method calls on the model
, controller
and view
.
Model:
model.setProperty("newProperty");
Controller:
@Override public void propertyChange(PropertyChangeEvent evt) { for (View view : views) { view.modelPropertyChange(evt); } }
View:
This result to the view being called as like:
@Override public void modelPropertyChange(PropertyChangeEvent evt) { if ("Property".equals(evt.getPropertyName())) { updateView(); } }
Question: How do verify(using EasyMock
in the JUnit
test), the expected order of method(with valid argument(s)) execution? I expect view.modelPropertyChange(evt)
to get called and the expect view.isViewUpdated()
to return true
on the view
object. How do I say that in my JUnit test? Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,
Controller.propertyChange()
不会调用View.isViewUpdated()
,因此无需模拟isViewUpdated
。您将在View
类的测试中测试isViewUpdated
。如果
propertyChange
确实调用了isViewUpdated
,那么您需要在EasyMock.replay()
之前添加以下调用:请注意
EasyMock.createMock()
不强制按照模拟的顺序调用模拟的方法。如果您希望强制执行方法顺序,请使用EasyMock.createStrictMock()
Note that the
Controller.propertyChange()
doesn't callView.isViewUpdated()
so there is no need to mockisViewUpdated
. You would testisViewUpdated
in a test for theView
class.If
propertyChange
did callisViewUpdated
then you would add the following call beforeEasyMock.replay()
:Note that
EasyMock.createMock()
does not enforce that the mocked methods be called in the order they were mocked. If you want the method order to be enforced, useEasyMock.createStrictMock()