【发布时间】:2010-12-21 20:28:24
【问题描述】:
我有一个使用 maven 构建的 spring 和 hibernate 持久性项目,我正在使用 Junit 和一个测试数据库 HSQLDB 运行测试,当我进行测试时首先初始化数据库 HSQLDB在服务器模式下,有没有办法让hudson初始化数据库,或者用maven?
【问题讨论】:
我有一个使用 maven 构建的 spring 和 hibernate 持久性项目,我正在使用 Junit 和一个测试数据库 HSQLDB 运行测试,当我进行测试时首先初始化数据库 HSQLDB在服务器模式下,有没有办法让hudson初始化数据库,或者用maven?
【问题讨论】:
我将以下内容添加到 pom 中。
<build>
<extensions>
<extension>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
</extension>
<extension>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>/src/main/java</outputDirectory>
</component>
</components>
<componentProperties>
<jdk5>true</jdk5>
<export>false</export>
<drop>true</drop>
<outputfilename>schema.sql</outputfilename>
</componentProperties>
</configuration>
<executions>
<execution>
<id>generate-ddl</id>
<phase>process-classes</phase>
<goals>
<!--Genera Esquema-->
<goal>hbm2ddl</goal>
<!--Genera Clases -->
<!-- <goal>hbm2java</goal> -->
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>create-schema</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>target/hibernate3/sql/schema.sql</srcFile>
</srcFiles>
</configuration>
</execution>
<execution>
<id>drop-db-after-test</id>
<phase>test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<sqlCommand>DROP SCHEMA public CASCADE</sqlCommand>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
</dependency>
</dependencies>
<configuration>
<driver>org.hsqldb.jdbcDriver</driver>
<username>sa</username>
<password></password>
<url>jdbc:hsqldb:file:etc/out/test.db;shutdown=true</url>
<autocommit>true</autocommit>
<skip>${maven.test.skip}</skip>
</configuration>
</plugin>
</plugins>
</build>
以下是我的数据源:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true"
destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:etc/out/test.db;shutdown=true" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
【讨论】:
我会为此使用DbUnit 和DbUnit Maven Plugin。您可以将其用于Clear database and insert a dataset before test phase 和/或为每个测试用例设置数据(请参阅 Getting Started 了解 JUnit 3,参阅 this blog 帖子,例如了解 JUnit 4)。
另一种选择是使用SQL Maven Plugin。 examples 部分的配置显示了如何删除/创建数据库和模式,然后在测试阶段之前填充它,并在测试阶段之后删除数据库)。
后一种方法使您对测试之间的数据设置的控制更少,这就是我更喜欢 DbUnit 的原因。
【讨论】:
DbUnit Maven Plugin 似乎只支持 DML 而不是 DDL。虽然SQL Maven Plugin 同时支持 DML 和 DDL - 这对于首先创建模式然后初始化数据非常方便,两者都通过 maven。
我们使用 Hudson 下的 Maven 执行此操作,其配置文件在 process-test-resources 阶段触发 maven-antrun-plugin。在我们的例子中,maven-antrun-plugin 运行一个生成模式的 Java 类,但当然还有其他选项。它看起来像这样:
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-database-schema-new</id>
<phase>process-test-resources</phase>
<configuration>
<tasks>
...
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
【讨论】: