I have this simplified DAO:
public final class SqlMapUserDao extends SqlMapClientDaoSupport implements UserDao
{
public User getUser(String username)
{
User user = (User)getSqlMapClientTemplate().queryForObject("getUser", username);
return user; } }
How do i test this without actually accessing the database ?
I'd have to create a mock of the sqlMapClientTemplate property so that i could make it return what i wanted for the test and not actually access the database, right ?
But how do i do this if SqlMapClientTemplate is not an interface ?
Thanks Hugo
Kris Jenkins wrote:
Hugo Palma wrote:
I'm using Spring with iBATIS SqlMap.
I'm trying to test my DAOs with junit and easyMock.
All my DAOs extend org.springframework.orm.ibatis.support.SqlMapClientDaoSupport, as such, for doing a database operation i invoke method getSqlMapClientTemplate().<whatever>.
What seemed like an obvious way to test the DAO was to create a mock of the sqlMapClientTemplate object(using easyMock) so that i could configure it's behaviour. But then the problems started. First i couldn't get the DAO instance from the spring application context without defining the "dataSource" and "configLocation" properties which i shouldn't have to define for the test because i'll be using a mock template anyway. I defined the properties anyway but then i came to a full stop as i realized that class org.springframework.orm.ibatis.SqlMapClientTemplate is in fact not an interface, this makes it impossible for easyMock to generate a mock object for it.
So my question is, is there any easy way i can this tests ? If you think this is more a spring list question please tell me.
Personally I wouldn't mock SqlMapClient, any more than I'd mock java.util.ArrayList. I only unit test my code, not 3rd party code. After all, it should already be thoroughly tested in development and the real world.
If you're prepared to accept that thinking, then testing each DAO in isolation from *your* other classes is easy - just pass a different sql-map-config.xml to Spring's SqlMapClientBuilder.
HTH, Kris
--
Hugo Palma Programador S�nior Digitalis Inform�tica Lda Telem: 938012004 [EMAIL PROTECTED] _ <mailto:[EMAIL PROTECTED]> _www.digitalis.pt_ <http://www.digitalis.pt/>_ _ <http://www.digitalis.pt/>

