Suppose I want to create and use an H2 database for my integration tests.
Maven has a command to run tests: mvn test
.
Is there a way to tell maven t
I create a file-based H2 database before unit tests are run. The file lives in the target
directory and can be removed at any time using mvn clean
.
I use the maven-sql-plugin as follows:
org.codehaus.mojo
sql-maven-plugin
1.5
com.h2database
h2
1.3.166
org.h2.Driver
jdbc:h2:file:target/db/testdb
sa
true
${maven.test.skip}
create-db
process-test-resources
execute
${sql.dir}/drop_db.sql
${sql.dir}/tables.sql
${sql.dir}/constraints.sql
... etc ...
The database can be created by running mvn process-test-resources
. When tests are run, make sure you connect to the database in target/db/testdb
via hibernate properties.
You will also need to a dependency on com.h2database.h2 in maven's dependencies.