ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Raible <li...@raibledesigns.com>
Subject Re: Dao unit testing?
Date Thu, 10 Feb 2005 17:09:41 GMT
Here's what I've used in the past.

     protected void setUp() throws Exception {
         DataSource ds = (DataSource) ctx.getBean("dataSource");
         conn = new DatabaseConnection(ds.getConnection());
         dataSet = new XmlDataSet(new  
FileInputStream("test/data/sample-data.xml"));
         // clear table and insert only sample data
         DatabaseOperation.CLEAN_INSERT.execute(conn, dataSet);
     }

     protected  void tearDown() throws Exception {
         // clear out database
         DatabaseOperation.DELETE.execute(conn, dataSet);
         conn.close();
         conn = null;
     }

In most cases, I've found using the Ant tasks is easier - then you can  
load up your database for a whole suite of tests, or just a single test  
- rather than each test re-loading the database.

Matt

On Feb 10, 2005, at 9:44 AM, Nathan Maves wrote:

> Kris,
>
> I am really close to getting everything to work with dbunit.   
> Everything works fine from an Ant task. I created my first Junit test  
> and this is where the trouble started.
>
> public class UserDaoTest extends DatabaseTestCase {
>     private static Log log = LogFactory.getLog(UserDaoTest.class);
>     private DaoManager daoManager = DaoConfig.getDaomanager();
>     private UserDao userDao = (UserDao)  
> daoManager.getDao(UserDao.class);
>     public UserDaoTest(String testName) {
>         super(testName);
>     }
>
>     protected IDatabaseConnection getConnection() throws Exception {
>         log.debug("Creating Connection");
>         Properties props =  
> Resources.getResourceAsProperties("giveservice/resources/ 
> database.properties");
>         SimpleDataSource dataSource = new SimpleDataSource(props);
>         IDatabaseConnection connection = new  
> DatabaseDataSourceConnection(dataSource);
>         DatabaseConfig config = connection.getConfig();
>          
> config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
>         return connection;
>     }
>
>     protected IDataSet getDataSet() throws Exception {
>         log.debug("Read DataSet");
>         return new  
> XmlDataSet(Resources.getResourceAsStream("dbunit/GST_USERS.xml"));
>     }
>
> 	testMothods.....
> }
>
> Everything runs fine but I can never see the data that should get  
> inserted into the db.  Hence all of my test fail!
>
> Does everything look ok to you?
>
> Can you send me any example that is like this?
>
> Nathan
>
> On Feb 10, 2005, at 8:56 AM, Kris Jenkins wrote:
>
>> Nathan Maves wrote:
>>
>>> I have been trying for two day to get DBunit to work with only  
>>> partial success.  The support for this project is nonexistent.   
>>> Anyone know of a more supported project or have any experience with  
>>> this one?  How do most of you test your database layer?
>>
>> I use DBunit, and I'm happy with it.  Have you seen this onjava  
>> article?
>>
>>    http://www.onjava.com/pub/a/onjava/2004/01/21/dbunit.html
>>
>> It got me started pretty quickly.  I could send you some sample files  
>> from one of my projects if you like, but you'll find that that  
>> article is much better documented. :-)
>>
>> Hope that helps,
>> Kris
>>
>> -- 
>> Kris Jenkins
>> Email:  kris@jenkster.com
>> Blog:   http://cafe.jenkster.com/
>> Wiki:   http://wiki.jenkster.com/
>>
>>


Mime
View raw message