You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Hugo Palma <hp...@digitalis.pt> on 2005/01/06 12:02:57 UTC

Can't test DAO

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.


Thanks

Hugo

Re: Can't test DAO

Posted by Clinton Begin <cl...@gmail.com>.
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 <hp...@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 _ <ma...@digitalis.pt>
> _www.digitalis.pt_ <http://www.digitalis.pt/>_ _ <http://www.digitalis.pt/>
> 
>

Re: Can't test DAO

Posted by Hugo Palma <hp...@digitalis.pt>.
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 _ <ma...@digitalis.pt>
_www.digitalis.pt_ <http://www.digitalis.pt/>_ _ <http://www.digitalis.pt/>


Re: Can't test DAO

Posted by Kris Jenkins <kr...@yahoo.co.uk>.
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

-- 
Kris Jenkins
Email:  kris@jenkster.com
Blog:   http://cafe.jenkster.com/
Wiki:   http://wiki.jenkster.com/



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.8 - Release Date: 03/01/2005