How can I EasyMock the cast operation?

How can I mock the cast operation. I have an cast operation on a dependent object , which will cast to another dependent object like

SqlMapClient sqlMapClient;
SqlMapClientImpl sqlMapClientImpl = (SqlMapClientImpl) sqlMapClient 

I'm mocking both the dependent clesses ie SqlMapClient and SqlMapClientImpl .But I need to know how to mock cast using EasyMock.

Any help would be appreciated.


You can't mock a cast, since a cast does not result in a method call on the object.

Instead, use EasyMock Class Extension to mock the SqlMapClientImpl class, and pass a reference to that mock to the class that takes in a SqlMapClient to a SqlMapClientImpl

Note, however, that doing a downcast like that in your code is a code smell. If your production code is doing a downcast of an interface to an implementation class, then you lose all of the flexibility of using an interface. It actually can be worse than not using an interface at all, since it looks like your class depends on the interface and can therefore be used with any implementation, but in actually your class depends on one specific implementation.


我们无法投射的原因是Easy Mock会动态地创建一个实现SqlMapClient类的类,并且它没有关于Implementation类(SqlMapClientImpl)的任何信息,因此一个便宜的技巧可能是创建一个实现SqlMapClient接口的类和扩展这可能工作的SqlMapClientImpl类。

链接地址: http://www.djcxy.com/p/47746.html

上一篇: iTunes for Windows的写法是什么?

下一篇: 我怎样才能EasyMock演员阵容?