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

Reply via email to