Maven环境下测试库和开发库的配置策略以及支持工具

在项目的开发和测试上,团队往往有这样一些情况和要求:

  1. 只要是走单元测试,必然要清库

  2. 开发人员不想每次重新构建都从一个空库开始重新输入数据,他们希望能够使用一个已有的库

  3. 如果新版本的数据库发生了结构上的变化,那么,有可能需要重新创建库,对于单元测试库也是如些,因此,开发人员希望新建开发库时能从自动导入一份基础数据,比如单元测试时使用的基础数据,这样就不必从头输入。

综合上述需求,可以这样配置:

  1. 单元测试和正常开发应该使用各自独立的库,单元测试库在每次进行单元测试时自动重建,不会保存任何数据,而开发库在build过程中一般保持不变,不会有重建操作。

  2. 单元测试的重建数据库工作应该发生在执行所有测试用例之前,是构建过程中的一个动作,而不是单元测试用例自己应该负责的。这与为每一个测试准备干净而一致的数据不同(如dbunit所要做的工作),这些操作是每一次测试中都会执行的。

  3. 构建工具需要提供手动重建开发库的功能,以便在需要的时候开发人员可以手动重新建库,并且开发库最好能有一些初始数据。

在Maven环境下,为满足以上需要,可以进行如下配置,使用到的插件是:Hibernate映射工具hbm2ddl和数据库单元测试工具dbunit.hbm2ddl可依据OR映射直接生成数据库schema并自动建库,而dbunit则可以将一份以文件形式(xml或excel)保存的基础数据导入数据库。以下是pom中hibernate插件的配置:

<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>hibernate3-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <id>default-cli</id> <goals> <goal>hbm2ddl</goal> </goals> <configuration> <components> <component> <name>hbm2ddl</name> <implementation>annotationconfiguration</implementation> </component> </components> <componentProperties> <drop>true</drop> <jdk5>true</jdk5> <format>true</format> <outputfilename>dbschema.sql</outputfilename> <propertyfile>target/classes/jdbc.properties</propertyfile> <skip>${skipTests}</skip> </componentProperties> </configuration> </execution> <execution> <id>for-unit-test</id> <phase>process-test-resources</phase> <goals> <goal>hbm2ddl</goal> </goals> <configuration> <components> <component> <name>hbm2ddl</name> <implementation>annotationconfiguration</implementation> </component> </components> <componentProperties> <drop>true</drop> <jdk5>true</jdk5> <format>true</format> <outputfilename>dbschema_for_test.sql</outputfilename> <propertyfile>target/test-classes/jdbc.properties</propertyfile> <skip>${skipTests}</skip> </componentProperties> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>${jdbc.groupId}</groupId> <artifactId>${jdbc.artifactId}</artifactId> <version>${jdbc.version}</version> </dependency> </dependencies> </plugin>
需要注意的是:这个配置中配置了两个execution,第一个execution是供手动调用的,没有绑定到任何phase上,而第二个execution是为单元测试准备的,它绑定到了process-test-resources阶段上,每次进行单元测试前都会执行重建数据库的工作。

以下是pom中dbunit插件的配置:

<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>dbunit-maven-plugin</artifactId> <version>1.0-beta-3</version> <dependencies> <dependency> <groupId>${jdbc.groupId}</groupId> <artifactId>${jdbc.artifactId}</artifactId> <version>${jdbc.version}</version> </dependency> </dependencies> <configuration> <driver>${jdbc.driverClassName}</driver> <url>${jdbc.url}&sessionVariables=foreign_key_checks=0</url> <username>${jdbc.username}</username> <password>${jdbc.password}</password> </configuration> <executions> <execution> <id>default-cli</id> <goals> <goal>operation</goal> </goals> <configuration> <type>CLEAN_INSERT</type> <src>src/test/resources/dbunit-test-data.xml</src> <dataTypeFactoryName>${dbunit.dataTypeFactory}</dataTypeFactoryName> <transaction>true</transaction> </configuration> </execution> </executions> </plugin>
这份配置中只有一个execution,是为手动调用而配置的,单元测试是以代码的方式调用dbunit的。在这份配置中要注意<url/>的值,它在标准url后追加了暂时关闭外键检查的参数,否则在导入数据有可能会失败。

完成上述配制后我们来看一下现在工程的构建过程:

1. 如何进行单元测试

当通过mvn test进行单元测试时,hbm2ddl会在单元测试前的准备阶段完成数据库的重建工作,每一个测试用例在执行前,则会通过dbunit导入一份标准数据,这部分工作是测试用例的@Before方法中实现的。

2. 如何手动建库

当开发人员需要手动建库时,只需要两个简单的命令就可以实现:

  通过hbm2ddl重建数据库:mvn hibernate3:hbm2ddl

  通过dbunit为新库导入基础数据:mvn dbunit:operation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值