如何使用 EasyMock 测试“调用树”当所有节点都是模拟的?
假设我有三种类型:TopHandler
、HandlerA
和 HandlerB
。 它们每个都有一个 void handle(Something)
方法。
TopHandler
的实例保存对 HandlerA
和 HandlerB
实例的引用,并且它在 上调用
和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据OP的描述,被模拟的主要接口是
Handler
接口:所有三个模拟对象都应实现该接口,并且TopHandler类是被测试的类:
测试用例必须验证< code>TopHandler 实现确实调用了 a 和 b 的
handle()
方法:According to the OP's description, the main interface which is mocked is the
Handler
interface:All three mocked objects shall implement this interface and the TopHandler class is the class under test:
The test case must verify that the
TopHandler
implementation indeed calls thehandle()
method of a and b:您不想模拟 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.