3

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

在 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>

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

提前致谢。

4

1 回答 1

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>

除了上述之外,这样的事情是集成测试而不是单元测试,但这是一个不同的问题/讨论。

于 2013-05-17T05:37:11.293 回答