让 EasyMock 模拟对象抛出异常
我正在使用 EasyMock 为许多协作类编写单元测试。其中一个类(我们称之为 Foo
)打开到远程服务器的网络连接,并将该服务器的 XML 响应解析为其他类可以使用的内容。
目前,我的测试仅涵盖一切顺利且远程服务器已启动并运行并按预期返回 XML 的场景。但是,如果我可以模拟 Foo
,我会更高兴,这样我就可以模拟远程服务器关闭时发生的情况,或者存在导致 的其他问题IOException
由 Foo
抛出。
我查看了 EasyMock API,没有看到任何看起来像要求模拟抛出异常的方法。
对我来说,进行基于 Exception
的测试并不是绝对必要的,但我很好奇 EasyMock 是否可以实现,并且我认为测试 Foo
的公共合约会很有用这样。
有人用 EasyMock 做过类似的事情吗?
参考
I'm in process of using EasyMock to write Unit tests for a number of collaborating classes. One of these classes (lets call it Foo
) opens a network connection to a remote server and parses that servers' XML response into something the rest of the classes can use.
Presently my tests only encompass scenarios in which everything is hunky-dory and the remote server is up and running and returning XML as expected. However, I would be happier if I could mock Foo
so that I simulate what happens if the remote server is down, or there is some other problem that causes an IOException
to be thrown by Foo
.
I have had a look at the EasyMock API, and I can't see anything that looks like a method asking a mock to throw an Exception.
It's not absolutely essential for me to have Exception
based tests, but I am curious if its possible with EasyMock, and I think it would be useful to test Foo
's public contract in this way.
Anyone done anything like this with EasyMock before?
References
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自文档:
例如:
From the documentation:
For example:
您可以在简单的模拟中使用方法
andThrow(Throwable throwable)
。查看文档 - 标题处理异常。例如
you can use the method
andThrow(Throwable throwable)
in easy mock. Check the documentation - heading Working with Exceptions.For example