Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DBUnit best practice

 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
thanks
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any help with his issue? anybody? thanks
 
Tim Cooke
Sheriff
Pie
Posts: 2972
123
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic