如何使用 EasyMock 测试“调用树”当所有节点都是模拟的?

发布于 2024-10-12 13:12:21 字数 433 浏览 3 评论 0原文

假设我有三种类型:TopHandlerHandlerAHandlerB。 它们每个都有一个 void handle(Something) 方法。

TopHandler 的实例保存对 HandlerAHandlerB 实例的引用,并且它在 上调用 handle() >HandlerAHandlerB

在我的测试中,我想模拟所有三个对象,然后验证对 topHandler 的调用是否会导致对其他两个对象的调用。

我该如何指定呢?我知道 EasyMock 可以让我指定模拟的预期行为,但我不清楚这里要使用哪些功能。

Suppose that I have three types: TopHandler, HandlerA, and HandlerB.
Each of them has a void handle(Something) method.

An instance of TopHandler holds references to instances of HandlerA and HandlerB, and it invokes handle() on HandlerA and HandlerB.

In my test I want to mock all three objects, and then verify that the invocation on topHandler would cause the invocation on the other two.

How do I specify that? I know that EasyMock lets me specify the expected behavior of the mocks, but I wasn't clear what features to use here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

盛夏已如深秋| 2024-10-19 13:12:21

根据OP的描述,被模拟的主要接口是Handler接口:

public interface Handler {
    void handle(String o);
}

所有三个模拟对象都应实现该接口,并且TopHandler类是被测试的类:

public static class TopHandler implements Handler {
    private Handler a;
    private Handler b;
    public TopHandler(Handler a, Handler b) {
        this.a = a; this.b = b;
    }
    public void handle(String o) {
        a.handle(o);
        b.handle(o);
    }
}

测试用例必须验证< code>TopHandler 实现确实调用了 a 和 b 的 handle() 方法:

@Test
public void testTopHandler() throws Exception {
    Handler mockA = EasyMock.createMock(Handler.class);
    mockA.handle("Test");
    EasyMock.expectLastCall().once();

    Handler mockB = EasyMock.createMock(Handler.class);
    mockB.handle("Test");
    EasyMock.expectLastCall().once();

    TopHandler handler = new TopHandler(mockA, mockB);
    EasyMock.replay(mockA, mockB);
    handler.handle("Test");
    EasyMock.verify(mockA, mockB);
}

According to the OP's description, the main interface which is mocked is the Handler interface:

public interface Handler {
    void handle(String o);
}

All three mocked objects shall implement this interface and the TopHandler class is the class under test:

public static class TopHandler implements Handler {
    private Handler a;
    private Handler b;
    public TopHandler(Handler a, Handler b) {
        this.a = a; this.b = b;
    }
    public void handle(String o) {
        a.handle(o);
        b.handle(o);
    }
}

The test case must verify that the TopHandler implementation indeed calls the handle() method of a and b:

@Test
public void testTopHandler() throws Exception {
    Handler mockA = EasyMock.createMock(Handler.class);
    mockA.handle("Test");
    EasyMock.expectLastCall().once();

    Handler mockB = EasyMock.createMock(Handler.class);
    mockB.handle("Test");
    EasyMock.expectLastCall().once();

    TopHandler handler = new TopHandler(mockA, mockB);
    EasyMock.replay(mockA, mockB);
    handler.handle("Test");
    EasyMock.verify(mockA, mockB);
}
妖妓 2024-10-19 13:12:21

您不想模拟 TopHandler,因为这似乎是您实际正在测试的类。在您的测试用例中,正常模拟其他两个并检查它们是否被调用。如果你模拟所有的类,就没有什么可以测试的。

如果您实际上有另一个正在测试的类调用 TopHandler,那么您只需验证是否在测试用例中调用了该类,因为其他调用仅是针对 TopHandler 的测试。

You do not want to mock TopHandler as that seems to be the class you are actually testing. In your test case, mock the other two as normal and check that they were called. If you mock all of your classes, there is nothing to test.

If you actually have a another class being tested that calls TopHandler then you only need to verify that this is called in the test case, as the other calls are a test for TopHandler only.

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