在 Maven/Junit/DBUnit 项目集成测试之前/之后创建/删除数据库的最佳方法?

发布于 2024-09-03 23:58:47 字数 99 浏览 14 评论 0原文

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

这里的最佳实践是什么?

I've seen some people use the maven-sql-plugin to do this. But it seems like a task that is better suited for DBUnit....perhaps at the beginning of an entire test suite.

What's the best practice here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2024-09-10 23:58:47

我使用 Maven SQL 插件

你最好使用它并确保在测试之前创建并填充,然后在测试之后删除。如果测试失败并留下数据库处于某种不一致的状态。

I use the Maven SQL Plugin

You're much better off using it and making sure that you create and populate before your tests and then drop after your tests. You'll also want to use create or replace, or drop if exists in your creation script (assuming your database supports it) in the event that a test fails and leaves the database in some inconsistent state.

梦中楼上月下 2024-09-10 23:58:47

我花了一些时间摆弄,但我让它删除、创建和创建 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>  

It took some fiddling around, but I got it to drop, create, and create the schema for H2 and MySQL. Still need to finish it for Oracle and SQL*Server 2008. I tucked the exact DROP and CREATE commands into properties and in some cases (such as H2) needed to skip the create database altogether. Here is what it looks like:

  <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>  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文