tags:

views:

31

answers:

1

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' mocking both the dependent clesses i.e SqlMapClient and SqlMapClientImpl .But I need to know how to mock cast using EasyMock.

Any help would be appreciated.

+2  A: 

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.

NamshubWriter