ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Clinton Begin <clinton.be...@gmail.com>
Subject Re: Can't test DAO
Date Thu, 06 Jan 2005 15:39:02 GMT
As far as iBATIS goes, testing/mocking SqlMapClient is very easy. 
Because it's an interface, you don't even really need a Mock
framework.

The problem you're seeing has more to do with Spring, and is a
question best asked of the Spring team....that is:  How do you mock
their SqlMapClientTemplate.

Cheers,
Clinton


On Thu, 06 Jan 2005 15:09:24 +0000, Hugo Palma <hpalma@digitalis.pt> wrote:
> I'm not sure i explained myself correctly. I think that with some code
> i'll explain myself better.
> 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
> _hpalma@digitalis.pt _ <mailto:hpalma@digitalis.pt>
> _www.digitalis.pt_ <http://www.digitalis.pt/>_ _ <http://www.digitalis.pt/>
> 
>

Mime
View raw message