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

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

问题描述

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

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

例如接口Foo和接口Closeable?

在 Rhino Mocks 中,你可以在创建 mock 对象时提供多个接口,但 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 来实现这一点,而无需求助于创建一个扩展 FooCloseable 的临时接口,然后对其进行模拟?

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.

顺便说一句,我闻到了一点代码的味道——如果一个方法真的将一个对象视为 2 个不同的东西,FooCloseable 接口这个案例?

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?

以这种方式构建代码将打开"和关闭"保持在同一个 try ... 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 创建一个实现多个接口的模拟对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:处理具有当前时间条件的单元测试 下一条:模拟;使用列表调用验证方法,忽略列表中元素的顺序

相关文章

最新文章