自动更新 Java 项目单元测试的数据库架构

发布于 2024-10-08 20:47:20 字数 1565 浏览 0 评论 0原文

我有一个 Java 项目,需要在执行单元测试之前使用新模式填充数据库。 Maven 用于构建项目。 Unitils 用于单元测试。

我的计划是按以下顺序执行以下脚本:

  1. 删除/创建数据库脚本 (dropcreate.sql)
  2. 架构脚本 (schema.sql)
  3. 参考数据脚本 (reference.sql)

作为一个附带好处,我可以查看参考数据脚本在执行失败时是否不再与架构匹配。

我使用 Maven 的 hibernate3 插件在构建时生成模式脚本。我还使用 maven-antrun-plugin 将这些脚本全部复制到同一目录 (target/dbscripts) 中,并按执行顺序排列数字,如下所示:

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

计划是让 unitils 的 DBMaintainer 部分运行脚本,如 Unitils 教程 中所述,作者:在 src/test/resources 中使用以下(经过清理的)unitils.properties 文件:

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 可以很好地加载数据集并进行单元测试,但它似乎从未运行脚本。我确信我忽略了一些简单的事情。

另一种方法是使用 sql-maven-plugin,但我无法让它在 process-test-resources 期间执行,所以我现在已经提出了这个想法。

如何让 Unitils 实际执行 SQL,并且如果可能的话,如果 SQL 无法执行则失败?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2024-10-15 20:47:20

您能提供更多信息吗?
日志里有什么东西吗?它应该提到类似的内容:
检查数据库是否需要更新。

您确定测试执行时 target/dbscripts 中有脚本吗?

是否加载了任何 updateDataBaseSchema 属性设置为 false 的unitils-local.properties?

现在,unitils 仍然使用它自己的 dbmaintainer 版本。在下一个版本中,将与 dbmaintain 本身完全集成(alpha 将于下周发布)。

请注意,dbmaintain 还具有运行数据库脚本的 Maven 任务。请访问 www.dbmaintain.org 了解更多信息。

brgds,
蒂姆

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

苏璃陌 2024-10-15 20:47:20

使用像 DBUnit 这样的东西会让生活更轻松吗?它负责创建和创建。用于测试的表的数量。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文