13

我见过一些人使用 maven-sql-plugin 来做到这一点。但这似乎是一个更适合 DBUnit 的任务......也许在整个测试套件的开头。

这里的最佳做法是什么?

4

2 回答 2

15

我使用Maven SQL 插件

最好使用它并确保在测试之前创建和填充,然后在测试之后删除。如果测试失败并使数据库处于某种不一致的状态,您还需要使用create 或 replace,或者如果存在于您的创建脚本中(假设您的数据库支持它)则 drop 。

于 2010-06-04T02:30:15.857 回答
13

它花了一些时间,但我让它为 H2 和 MySQL 删除、创建和创建模式。仍然需要为 Oracle 和 SQL*Server 2008 完成它。我将确切的 DROP 和 CREATE 命令放入属性中,并且在某些情况下(例如 H2)需要完全跳过创建数据库。这是它的样子:

  <plugin>
    <!-- Used to automatically drop (if any) and create a database prior to running integration test cases. -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    <dependencies>
      <dependency>
        <!-- Adds the correct JDBC driver as a dependency of this plugin -->
        <groupId>${database.groupId}</groupId>
        <artifactId>${database.artifactId}</artifactId>
        <version>${database.version}</version>
      </dependency>
    </dependencies>
    <configuration>
      <!-- common configuration shared by all executions -->
      <driver>${database.class}</driver>
      <username>${database.username}</username>
      <password>${database.password}</password>
      <url>${database.url}</url>
    </configuration>
    <executions>
      <execution>
        <!-- Start by dropping the database (we'll leave it intact when finished) -->
        <id>drop-db</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <!-- Can't use regular URL in case database doesn't exist -->
          <url>${database.url.alternate}</url>
          <skip>${database.sqlDrop.skip}</skip>
          <autocommit>true</autocommit>
          <sqlCommand>${database.sqlDrop};</sqlCommand>
          <onError>continue</onError>
        </configuration>
      </execution>
      <execution>
        <!-- then create a new database -->
        <id>create-db</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <!-- Can't use regular URL in case database doesn't exist -->
          <url>${database.url.alternate}</url>
          <skip>${database.sqlCreate.skip}</skip>
          <autocommit>true</autocommit>
          <sqlCommand>${database.sqlCreate};</sqlCommand>
          <onError>continue</onError>
        </configuration>
      </execution>
      <execution>
        <!-- and finally run the schema creation script we just made with the hibernate3-maven-plugin -->
        <id>create-schema</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <skip>${database.sqlSchema.skip}</skip>
          <autocommit>true</autocommit>
          <srcFiles>
            <srcFile>target/hibernate3/sql/create-${database.vendor}-schema.sql</srcFile>
          </srcFiles>
          <onError>continue</onError>
        </configuration>
      </execution>
    </executions>
  </plugin>  
于 2010-06-04T05:32:51.533 回答