简体   繁体   English

Unitils / DbUnit:如何在多数据库环境中进行测试?

[英]Unitils/DbUnit: How to test in a multi-database environment?

I'm using Unitils (with DbUnit) for my data access layer unit testing, but the need has arisen to test multiple databases. 我在数据访问层单元测试中使用Unitils(带有DbUnit),但是已经出现了测试多个数据库的需求。 What's the best way to do it? 最好的方法是什么? The databases are different so some DAOs are for one database, another DAOs are for another. 数据库是不同的,因此某些DAO用于一个数据库,另一个DAO用于另一个数据库。

I see the following alternatives: 我看到以下选择:

  1. Associating each *DaoTest with a separate unitils.properties file that would hold configuration for this DAO's database. 将每个* DaoTest与一个单独的unitils.properties文件相关联,该文件将保存此DAO数据库的配置。 Is it even possible? 可能吗?
  2. Having a separate test project for every database (holding this database's *DaoTests and a unitils.properties file with the database's credentials) 每个数据库都有一个单独的测试项目(保存该数据库的* DaoTests和一个带有数据库凭据的unitils.properties文件)

Any other ideas? 还有其他想法吗?

Hopefully you found an answer for this in the 6 years since you originally asked :) 希望您在最初提出问题的6年内找到了答案:)

I recently found myself with this same issue and resolved it this way: 我最近发现自己也遇到了同样的问题,并通过以下方式解决了这个问题:

I used a single unittils.properties for each DAO which defines every datasource my project needs to test. 我为每个DAO使用了一个unittils.properties ,它定义了我的项目需要测试的每个数据源。 In the unittils.properties file, I defined a database.schemaNames=DATABASE_1, DATABASE_2 property. unittils.properties文件中,我定义了一个database.schemaNames=DATABASE_1, DATABASE_2属性。

Then, you can modify your dataset definition to look something like this: 然后,您可以修改数据集定义,如下所示:

<?xml version='1.0' encoding='UTF-8'?>
<dataset xmlns="DATABASE_1" xmlns:b="DATABASE_2">
    <some_table />
    <b:some_other_table />

    <some_table attr_1="foo" attr_2="bar" />
    <b:some_other_table other_attr="baz" />
</dataset>

Note that some_table will be assumed to live in DATABASE_1 . 注意,将假定some_table位于DATABASE_1 This is because unittils sets the first database in the database.schemaNames property as the default. 这是因为unittils将database.schemaNames属性中的第一个数据库设置为默认database.schemaNames You can optionally omit the xmlns="DATABASE_1" in your dataset's xml file because of this. 因此,您可以选择省略数据集的xml文件中的xmlns="DATABASE_1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM