【问题标题】:Unit Testing Postgres Database with Maven使用 Maven 对 Postgres 数据库进行单元测试
【发布时间】:2013-05-17 01:25:50
【问题描述】:

首先让我解释一下我在做什么:

在 Eclipse (JUNO) 中创建一个 Java 项目以对 Postgres 数据库的 SQL 进行单元测试。

使用 Maven2 作为构建工具。我有 DBUnit 和 SQL maven 插件。

目标是删除架构并重建表并在表中加载一些数据。

我已经测试了 SQL,所以我知道它可以工作。我已经测试了连接,所以我知道 URL 是正确的。

现在谈谈我的问题。我是 Maven 的单元测试 SQL 新手。我试图遵循大多数在线文档。我从示例中创建了我的 pom。在编译 Java 代码时,构建会受到 pom.xml 文件中的 SQL 工作的影响。这是我的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.premierinc.esd</groupId>
<artifactId>sqlunittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sqlunittest</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>3.8.1</junit.version>
    <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
    <sql.maven.plugin.version>1.5</sql.maven.plugin.version>
    <postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version>
</properties>

<repositories>
    <repository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <layout>default</layout>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <layout>default</layout>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <updatePolicy>never</updatePolicy>
        </releases>
    </pluginRepository>
</pluginRepositories>


<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.jdbc.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>${sql.maven.plugin.version}</version>

                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:testdb</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!--all executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>


                <executions>
                    <execution>
                        <id>drop-schema-before-test-if-any</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <!-- need another database to drop the targeted one -->
                            <url>jdbc:postgresql://localhost:5432:postgres</url>
                            <autocommit>true</autocommit>
                            <sqlCommand>DROP SCHEMA chipen CASCADE</sqlCommand>
                            <!-- ignore error when database is not available -->
                            <onError>continue</onError>
                        </configuration>
                    </execution>


                    <execution>
                        <id>create-schema</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>src/main/sql/CHI-PEN-schema.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>dbunit-maven-plugin</artifactId>
                <version>1.0-beta-3</version>


                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:testdb</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!--all executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>

                    <execution>

                        <phase>test-compile</phase>
                        <goals>
                            <goal>operation</goal>
                        </goals>
                        <!-- specific configurations -->
                        <configuration>
                            <type>CLEAN_INSERT</type>
                            <src>src/test/data/testdb_chipen_data.xml</src>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
        </plugins>
    </pluginManagement>
</build>

任何建议或提示将不胜感激。

提前致谢。

【问题讨论】:

  • 你说blow by the SQL,是不是说SQL没有执行?
  • 首先,为什么要在 pom 中定义默认值(存储库)?此外,您真的在命令行上使用 Maven 2 吗?在 Juno 中,您已经在使用 Maven 3,所以最好在命令行和 Eclipse Juno 中使用 Maven 3。
  • 您可能会发现这个新插件很有用:github.com/adrianboimvaser/postgresql-maven-plugin

标签: sql eclipse unit-testing maven dbunit


【解决方案1】:

我在此处以更正的形式添加您帖子的 pom。我特别删除了存储库定义,因为它们是 Maven 中的默认值,因此配置约定意味着只定义真正需要定义的内容。 此外,我已经删除了 pluginManagement 标签,因为这意味着不会真正执行它所定义的东西。更准确地说,pluginManagement 旨在定义插件的版本,但通常不是配置。这通常用于父 pom 中:

<project ...>
  ..
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
           Plugin groupId, artifactId, version
        </plugin>
        .
      </plugin>
    </pluginManagement>
    ..
</project>

让我们回到你的 pom。以下应该运行:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.premierinc.esd</groupId>
<artifactId>sqlunittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sqlunittest</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>3.8.1</junit.version>
    <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version>
    <sql.maven.plugin.version>1.5</sql.maven.plugin.version>
    <postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.jdbc.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>${sql.maven.plugin.version}</version>

                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:testdb</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!--all executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>
                    <execution>
                        <id>drop-schema-before-test-if-any</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <!-- need another database to drop the targeted one -->
                            <url>jdbc:postgresql://localhost:5432:postgres</url>
                            <autocommit>true</autocommit>
                            <sqlCommand>DROP SCHEMA chipen CASCADE</sqlCommand>
                            <!-- ignore error when database is not available -->
                            <onError>continue</onError>
                        </configuration>
                    </execution>


                    <execution>
                        <id>create-schema</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>src/main/sql/CHI-PEN-schema.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>dbunit-maven-plugin</artifactId>
                <version>1.0-beta-3</version>


                <dependencies>
                    <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.jdbc.version}</version>
                    </dependency>
                </dependencies>

                <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://localhost:5432:testdb</url>
                    <username>postgres</username>
                    <password>root</password>
                    <!-- You can comment out username/password configurations and have 
                        maven to look them up in your settings.xml using ${settingsKey} -->
                    <settingsKey>sensibleKey</settingsKey>
                    <!--all executions are ignored if -Dmaven.test.skip=true -->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>

                    <execution>

                        <phase>test-compile</phase>
                        <goals>
                            <goal>operation</goal>
                        </goals>
                        <!-- specific configurations -->
                        <configuration>
                            <type>CLEAN_INSERT</type>
                            <src>src/test/data/testdb_chipen_data.xml</src>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
        </plugins>
</build>

除了上述之外,还有集成测试和不是单元测试,但这是一个不同的问题/讨论。

【讨论】:

  • 好的。谢谢大家的建议和建议。我真的很感激。我已经删除了插件管理元素以及本地存储库。这样就可以执行 SQL 脚本了。
  • 除了使用 sql-maven-plugin 之外,您还有什么理由应该使用 dbunit-maven-plugin 而不是像 this example 中那样只使用 sql-maven-plugin?
猜你喜欢
  • 1970-01-01
  • 2017-10-13
  • 2019-11-15
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
相关资源
最近更新 更多