Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I EasyMock the cast operation?

Tags:

java

easymock

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

Any help would be appreciated.

like image 377
user441756 Avatar asked Sep 07 '10 19:09

user441756


1 Answers

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.

like image 168
NamshubWriter Avatar answered Oct 06 '22 02:10

NamshubWriter