No problem.

The beauty of the Spring transactional manager is you don't have to
manage it.  It maintains a transaction for the entire test, so any
parent records created inside the test method via inserts are all part
of the same transaction as the child records. Hence, you should not
receive any foreign key errors.  Spring will automatically roll it all
back, without the need to denote transactional boundaries using
annotations. 

This leaves you free to test your ibatis code without other moving parts
getting in the way.  

Glad to offer something back to the iBatis community for the help I
received from others!

-Kevin

-----Original Message-----
From: David Brown [mailto:dbr...@sexingtechnologies.com] 
Sent: July 8, 2009 5:42 PM
To: user-java@ibatis.apache.org
Subject: Re: Begin book addresses mock object testing only
(alternatives?)


Hello Kevin, thanks for the reply (helpful and informative). I am using
Spring for the JUnit testing but all via imports. And, I am using
annotations: @Rollback, @Test and @Transactional. I have annotated:
@Rollback(value=true) as this has been the problem: rollbacks. My
problem is the record is inserted into the parent table that generates a
key needed by a the child table record insert. Though Rollbacks are
disabled the transaction gets committed and the newly generated key
disappears leading to an exception.

I will create an all-new test class and use your abstract.

Thanks and regards, David.



----- Original Message -----
From: "Burke.Kevin" <kevin.bu...@cic.gc.ca>
To: user-java@ibatis.apache.org
Sent: Wednesday, July 8, 2009 12:57:26 PM GMT -06:00 US/Canada Central
Subject: RE: Begin book addresses mock object testing only
(alternatives?)

David,

If you are using Spring to wire up your iBatis configuration, there is
an abstract Spring class for performing DAO tests that encapsulates
transaction management around each test. You can perform the necessary
iBatis calls, and Spring will rollback all of the test changes. 

If you require more details on this class, see the Spring API Javadocs
(http://static.springsource.org/spring/docs/2.5.x/api/index.html)

Here is an example base class I use for all of my DAO tests. Simply
subclass this for each test class, ensuring that if you override the
onSetUp() or onTearDown() methods (instead of the jUnit setUp() or
tearDown() superclass methods), you also invoke the super.onSetUp() or
super.onTearDown() methods from within.  I use a separate
applicationContext file for these testsm as noted below.

Hope this helps,

-Kevin



------- Code Sample --------

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import
org.springframework.test.AbstractTransactionalDataSourceSpringContextTes
ts;

/**
 * Abstract base class for DAO tests that require rollback functionality
when testing 
 * CRUD operations.
 * @author Kevin.Burke
 *
 */
public  abstract class  AbstractBaseDaoTestCase
                extends
AbstractTransactionalDataSourceSpringContextTests
{
        protected Log   log     = LogFactory.getLog( this.getClass() );
        

        public AbstractBaseDaoTestCase()
        {
                super();
        }
        
    protected String[] getConfigLocations()
    {
        String[] paths = new String[] {
                "classpath:applicationContext-db-test.xml"
          };
        return paths;
    }
    
    public Object getBean( String beanName )
    {
        return getApplicationContext().getBean( beanName );
    }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscr...@ibatis.apache.org
For additional commands, e-mail: user-java-h...@ibatis.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscr...@ibatis.apache.org
For additional commands, e-mail: user-java-h...@ibatis.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscr...@ibatis.apache.org
For additional commands, e-mail: user-java-h...@ibatis.apache.org

Reply via email to