wood burning stoves
The moose likes Testing and the fly likes DBUnit best practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "DBUnit best practice" Watch "DBUnit best practice" New topic

DBUnit best practice

David Spades
Ranch Hand

Joined: Feb 01, 2014
Posts: 342

I wish to know how to use DBUnit 2.5 to test database access.
1. In PROD, I use Oracle, surely that doesn't mean I have to use Oracle for unit testing right? I'm assuming HSQLDB is good enough. is it possible to use HSQLDB to mock Oracle? I mean so that HSQLDB can process Oracle's syntax?
2. Which class to extend when writing test classes? xxxTestCase or xxxDatabaseTester?
3. What is the recommended way to initialize the database (create and populate the database objects)? I'd prefer to put external file for all DDL and DML needs.
I'm still confused on how to put the three parts together.
David Spades
Ranch Hand

Joined: Feb 01, 2014
Posts: 342
any help with his issue? anybody? thanks
Tim Cooke

Joined: Mar 28, 2008
Posts: 2519

1. You don't have to use Oracle for your automated testing, you can use an in memory DB as you suggest. But... this assumes that you are not using any Oracle specific syntax or data types in your schema, in which case you're stuck with Oracle. Although it would be worth checking to see if your Oracle db has an in memory mode that you could use for local testing and CI.

2. Pass. Refer to the DBUnit documentation to see which is suitable for your purposes.

3. My experience with DBUnit has required that the database already be created with the correct schema. Then for your tests you have an xml file that describes the content you want loaded into it.

It's always a good idea to have tests in place that verify the correct integration with the actual database you're going to be using. As these would be Integration Tests you can expect them to be slow and a bit of a pest to setup, i.e. having to make a real Oracle db available. Due to this I would keep the number of tests relatively small, just enough to give you confidence that it's ok. However for your application Unit Tests, of which there would be much more, I would usually swap out the DAO classes with fakes either with a Mocking framework such as Mockito or even just write a test double version that is backed by a Map. That way your Unit Tests are fast and require no external resources.

Tim Driven Development
David Spades
Ranch Hand

Joined: Feb 01, 2014
Posts: 342
yes, for unit tests for all my objects other than DAO objects, I use fakes. the problem is, now I want to test DAO objects, so DBUnit it is. thanks
I agree. Here's the link: http://aspose.com/file-tools
subject: DBUnit best practice
It's not a secret anymore!