让 EasyMock 模拟对象抛出异常

发布于 2024-10-17 17:22:30 字数 591 浏览 6 评论 0原文

我正在使用 EasyMock 为许多协作类编写单元测试。其中一个类(我们称之为 Foo)打开到远程服务器的网络连接,并将该服务器的 XML 响应解析为其他类可以使用的内容。

目前,我的测试仅涵盖一切顺利且远程服务器已启动并运行并按预期返回 XML 的场景。但是,如果我可以模拟 Foo,我会更高兴,这样我就可以模拟远程服务器关闭时发生的情况,或者存在导致 的其他问题IOExceptionFoo 抛出。

我查看了 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 技术交流群。

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

发布评论

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

评论(2

好久不见√ 2024-10-24 17:22:30

来自文档

用于指定要抛出的异常(更准确地说:Throwable),由 expectLastCall()expect(T value) 返回的对象 提供方法andThrow(Throwable throwable)。在调用指定要抛出的 Throwable 的模拟对象之后,必须在记录状态下调用该方法。

每个方法都可以抛出未经检查的异常(即,RuntimeExceptionError 及其所有子类)。检查异常只能从实际抛出异常的方法中抛出。

例如:

expectLastCall().andThrow(new HibernateException("Something terrible happened"));

expect(query.list()).andThrow(
        new HibernateException("Something terrible happened"));

From the documentation:

For specifying exceptions (more exactly: Throwables) to be thrown, the object returned by expectLastCall() and expect(T value) provides the method andThrow(Throwable throwable). The method has to be called in record state after the call to the Mock Object for which it specifies the Throwable to be thrown.

Unchecked exceptions (that is, RuntimeException, Error and all their subclasses) can be thrown from every method. Checked exceptions can only be thrown from the methods that do actually throw them.

For example:

expectLastCall().andThrow(new HibernateException("Something terrible happened"));

expect(query.list()).andThrow(
        new HibernateException("Something terrible happened"));
爱要勇敢去追 2024-10-24 17:22:30

您可以在简单的模拟中使用方法andThrow(Throwable throwable)。查看文档 - 标题处理异常

例如

 expect(mock.voteForRemoval("Document"))
    .andThrow(new RuntimeException(), 4);

you can use the method andThrow(Throwable throwable) in easy mock. Check the documentation - heading Working with Exceptions.

For example

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