Search code examples
javasqljunithsqldb

Best way to clone existing DB for Junit tests?


I have to clone tables from an existing database somehow in order to run unit tests. The ideal solution would be something - a Maven plugin or Spring bean - that would clone the database tables into a HSQL database, which I could then hit with my JUnit tests.

What is the most straightforward way to do this?


Solution

  • Download DBUnit. http://dbunit.sourceforge.net/

    Export a dataset from your existing DB to XML.

    In your suite, import the dataset into HSQL.

    You're going to want to get to know DBUnit if you intend to unit test an app whose behavior is tightly coupled with its persistence layer (e.g. anything written in ORM).