是否可以创建一个使用EasyMock实现多个接口的模拟对象? [英] Is it possible to create a mock object that implements multiple interfaces with EasyMock?

查看:209
本文介绍了是否可以创建一个使用EasyMock实现多个接口的模拟对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个使用EasyMock实现多个接口的模拟对象?

Is it possible to create a mock object that implements several interfaces with EasyMock?

例如,接口 Foo 和接口可关闭

在Rhino Mocks中,您可以在创建模拟对象时提供多个接口,但是EasyMock的 createMock()方法只需要一种类型。

In Rhino Mocks you can provide multiple interfaces when creating a mock object, but EasyMock's createMock() method only takes one type.

是否可以使用EasyMock实现这一点,而无需借助后备创建一个临时接口,扩展 Foo 可关闭,然后嘲笑它?

Is it possbile to achieve this with EasyMock, without resorting to the fallback of creating a temporary interface that extends both Foo and Closeable, and then mocking that?

推荐答案

EasyMock不支持此功能,因此您会遇到临时接口的后备问题。

EasyMock doesn't support this so you're stuck with fallback of the temporary interface.

顺便说一句,我闻到了一些代码wiff - 如果一个方法真的将对象视为两个不同的东西, Foo 在这种情况下可关闭界面?

As an aside, I smell a little bit of a code wiff - should a method really be treating an object as 2 different things, the Foo and Closeable interface in this case?

这意味着该方法正在执行多个操作,而我怀疑其中一个操作是关闭 Closeable ,调用代码决定是否需要'关闭'是不是更有意义?

This implies to me that the method is performing multiple operations and while I suspect one of those operations is to 'close' the Closeable, wouldn't it make more sense for the calling code to decide whether or not the 'close' is required?

以这种方式构建代码可使'open'和'close'保持在同一中尝试... finally 块和恕我直言使代码更具可读性,更不用说更通用的方法,并允许您传递仅实现 Foo 的对象。

Structuring the code this way keeps the 'open' and 'close' in the same try ... finally block and IMHO makes the code more readable not to mention the method more general and allows you to pass objects that only implement Foo.

这篇关于是否可以创建一个使用EasyMock实现多个接口的模拟对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆