如何使用 JUnit 和 EasyMock 在 MVC 集成测试中模拟视图

发布于 2024-09-18 17:32:12 字数 883 浏览 6 评论 0原文

我想模拟 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我ぃ本無心為│何有愛 2024-09-25 17:32:12
@RunWith(JUnit4.class)
public class ControllerTest {
  @Test
  public void updateView() {
    PropertyChangeEvent evt = new PropertyChangeEvent( ... );
    View mockView = EasyMock.createMock(View.class);
    mockView.modelPropertyChange(evt);
    EasyMock.replay(mockView);

    Controller controller = new Controller( ... );
    controller.propertyChange(mockView);
    EasyMock.verify(mockView);
  }
}

请注意,Controller.propertyChange() 不会调用 View.isViewUpdated(),因此无需模拟 isViewUpdated。您将在 View 类的测试中测试 isViewUpdated

如果 propertyChange 确实调用了 isViewUpdated,那么您需要在 EasyMock.replay() 之前添加以下调用:

EasyMock.expect(mockView.isViewUpdated()).andReturn(true);

请注意 EasyMock.createMock() 不强制按照模拟的顺序调用模拟的方法。如果您希望强制执行方法顺序,请使用 EasyMock.createStrictMock()

@RunWith(JUnit4.class)
public class ControllerTest {
  @Test
  public void updateView() {
    PropertyChangeEvent evt = new PropertyChangeEvent( ... );
    View mockView = EasyMock.createMock(View.class);
    mockView.modelPropertyChange(evt);
    EasyMock.replay(mockView);

    Controller controller = new Controller( ... );
    controller.propertyChange(mockView);
    EasyMock.verify(mockView);
  }
}

Note that the Controller.propertyChange() doesn't call View.isViewUpdated() so there is no need to mock isViewUpdated. You would test isViewUpdated in a test for the View class.

If propertyChange did call isViewUpdated then you would add the following call before EasyMock.replay():

EasyMock.expect(mockView.isViewUpdated()).andReturn(true);

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, use EasyMock.createStrictMock()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文