3

まず、私が何をしているのかを説明しましょう:

Eclipse (JUNO) で Java プロジェクトを作成して、Postgres データベースの SQL を単体テストします。

Maven2 をビルド ツールとして使用します。DBUnit と SQL Maven プラグインがあります。

目標は、スキーマを削除してテーブルを再構築し、テーブルにデータをロードすることです。

私はSQLをテストしたので、それが機能することを知っています。接続をテストしたので、URL が正しいことがわかりました。

今私の問題に。SQL の単体テスト用に Maven を初めて使用します。私はほとんどのオンライン ドキュメントに従おうとしました。例からポンポンを作成しました。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

ここにあなたの投稿のポンポンを修正した形で追加します。特にリポジトリ定義を削除しました。これらは Maven のデフォルトであるため、構成に関する規則は、本当に定義する必要があるものだけを定義することを意味します。さらに、pluginManagementタグを削除しました。これは、定義することを意味することを実際に実行しないことを意味します。より正確に言うと、 pluginManagementはプラグインのバージョンを定義することを目的としていますが、通常は構成を定義することは意図していません。これは通常、親 pom で使用されます。

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

ポンポンに戻りましょう。以下が実行されます。

<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 に答える