开发者

Automatically Updating Database Schema For Java Project's Unit Tests

I've got a Java project that needs to populate the database with a new schema prior to executing Unit Tests. Maven is used to build the project. Unitils is used for the unit tests.

My plan was to execute the following scripts, in this order:

  1. Drop/Create database script (dropcreate.sql)
  2. Schema script (schema.sql)
  3. Reference data script (reference.sql)

As a side benefit I can see if the reference data script no longer matches the schema if it fails to execute.开发者_开发知识库

I have used the hibernate3 plugin for Maven to generate the schema script at build time. I've also used the maven-antrun-plugin to copy these scripts all into the same directory (target/dbscripts) with numbers in order of execution, like so:

  • 001_dropcreate.sql
  • 002_schema.sql
  • 003_reference.sql

The plan was to have the DBMaintainer portion of unitils run the scripts, as noted at Unitils Tutorial, by using the following (sanitised) unitils.properties file in src/test/resources:

database.driverClassName=com.sybase.jdbc3.jdbc.SybDriver
database.url=jdbc:sybase:Tds:mydatabase.server.example.com:9000/my_database
database.userName=myusername
database.password=mypassword
database.dialect=org.hibernate.dialect.SybaseDialect
database.schemaNames=dbo
DatabaseModule.Transactional.value.default=disabled
org.unitils.core.dbsupport.DbSupport.implClassName=org.unitils.core.dbsupport.MsSqlDbSupport
DbUnitModule.DataSet.loadStrategy.default=com.example.dao.SybaseCleanInsertLoadStrategy
updateDataBaseSchema.enabled=true
dbMaintainer.script.locations=target/dbscripts
dbMaintainer.cleanDb.enabled=true
dataSetStructureGenerator.xsd.dirName=target/xsd

Unitils loads datasets and conducts the unit tests fine, but it never seems to run the scripts. I'm sure it's something simple I'm overlooking.

An alternative was to use the sql-maven-plugin, but I couldn't get it to execute during process-test-resources, so I've tabled that idea for now.

How do I get Unitils to actually execute the SQL, and, if possible, fail if the SQL cannot be executed?


Could you give some more info? Was there something in the logs? It should mention something like:
Checking if database has to be updated.

Are you sure that there are scripts in target/dbscripts at the moment of test execution?

Was there any unitils-local.properties loaded in which the updateDataBaseSchema property was set to false?

Right now unitils still uses it's own version of the dbmaintainer. In the next release there is going to be full integration with dbmaintain itself (an alpha is comming out next week).

Note that dbmaintain also has maven tasks to run database scripts. Take a look at www.dbmaintain.org for more info.

brgds, Tim


Would using something like DBUnit make life easier? It looks after creation & population of tables for testing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜