在常规的内置Maven生命周期(jar,war …)中,
pre-integration-test和
post-integration-test测试阶段未绑定到任何maven插件(即,这些阶段的默认行为是“不执行任何操作”)。如果要为该
integration-test阶段中执行的测试设置并填充数据库,则需要将执行该工作的Maven插件绑定到这些阶段。
在SQL Maven插件在Maven构建执行SQL脚本。将此插件绑定到的配置
pre/post-integration-phase非常简单:
在pom.xml文件的
build>
plugins部分中,添加sql-maven-plugin
<plugin> <groupId>org.prehaus.mojo</groupId> <artifactId>sql-maven-plugin</artifactId> <version>1.5</version> <dependencies> <!-- include the JDBC driver dependency here --> <dependency> <groupId>...</groupId> <artifactId>...</artifactId> <version>...</version> </dependency> </dependencies> <!-- common plugin configuration --> <configuration> <driver>...</driver> <url>...</url> <username>...</username> <password>...</password> <!-- other parameters --> </configuration> <!-- the executions section binds the phases with some plugin goals and optional additional configuration parameters --> <executions> <execution> <phase>pre-integration-test</phase> <goals> <goal>execute</goal> </goals> <!-- specific configuration for this execution --> <configuration> <!-- Include here the SQL scripts to create the DB, inject some test data --> </configuration> </execution> <execution> <phase>post-integration-test</phase> <goals> <goal>execute</goal> </goals> <configuration> <!-- Include here the SQL scripts to drop the database --> </configuration> </execution> [...] </executions> </plugin>
这应该够了吧。



