4

Mavenプロジェクトの完全に自動化された統合テストが必要です。統合テストでは、実行する前に外部(プラットフォームに依存する)プログラムを開始する必要があります。理想的には、単体テストが終了した後に外部プログラムを強制終了しますが、必須ではありません。

これを実現するためのMavenプラグインはありますか?他のアイデア?

4

5 に答える 5

4

antrunプラグインを使用できます。内部では、ant のexec applyタスクを使用します。

このようなもの。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.2</version>
    <executions>
      <execution>
        <phase> <!-- a lifecycle phase --> </phase>
        <configuration>

          <tasks>
            <apply os="unix" executable="cmd">
              <arg value="/c"/>
              <arg value="ant.bat"/>
              <arg value="-p"/>
            </apply>
            <apply os="windows" executable="cmd.exe">
              <arg value="/c"/>
              <arg value="ant.bat"/>
              <arg value="-p"/>
            </apply>
          </tasks>

        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Ant はもちろん、条件タスクを通じて OS 固有のコマンドをサポートします。

于 2008-10-06T18:48:44.927 に答える
2

サーブレット開発を行っていて、統合テストのために結果のWARをデプロイしたい場合は、cargomavenプラグインが適しています。

私がこれを自分で行うとき、私はしばしばマルチモジュールプロジェクトをセットアップし(厳密には必須ではありませんが)、すべての統合テストをその1つのモジュールにカプセル化します。次に、プロファイルを使用してモジュールを有効にし(または有効にしないで)、すぐに「ええ、私はそれを壊したことを知っています」ビルドをブロックしません。

これがその機能テストモジュールからのpomです-それをあなたがするものにしてください:

<?xml version="1.0"?><project>
  <parent>
    <artifactId>maven-example</artifactId>
    <groupId>com.jheck</groupId>
    <version>1.5.0.4-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jheck.example</groupId>
  <artifactId>functional-test</artifactId>
  <name>Example Functional Test</name>
  <packaging>pom</packaging>

  <dependencies>
    <dependency>
      <groupId>com.jheck.example</groupId>
      <artifactId>example-war</artifactId>
      <type>war</type>
      <scope>provided</scope>
      <version>LATEST</version>
    </dependency>
    <dependency>
      <groupId>httpunit</groupId>
      <artifactId>httpunit</artifactId>
      <version>1.6.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
          <execution>
            <phase>integration-test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <version>0.3</version>
        <configuration>
          <wait>false</wait> <!-- don't pause on launching tomcat... -->
          <container>
            <containerId>tomcat5x</containerId>
            <log>${project.build.directory}/cargo.log</log>
            <zipUrlInstaller>
              <!--
              <url>http://www.apache.org/dist/tomcat/tomcat-5/v5.0.30/bin/jakarta-tomcat-5.0.30.zip</url>
               -->
               <!-- better be using Java 1.5... -->
              <url>http://www.apache.org/dist/tomcat/tomcat-5/v5.5.26/bin/apache-tomcat-5.5.26.zip</url>

              <installDir>${installDir}</installDir>
            </zipUrlInstaller>
          </container>
          <configuration>
            <!-- where the running instance will be deployed for testing -->
            <home>${project.build.directory}/tomcat5x/container</home>
          </configuration>
        </configuration>

        <executions>
          <execution>
            <id>start-container</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start</goal>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <deployer>
                <deployables>
                  <deployable>
                    <groupId>com.jheck.example</groupId>
                    <artifactId>example-war</artifactId>
                    <type>war</type>
                    <!-- <properties>
                      <plan>${basedir}/src/deployment/geronima.plan.xml</plan>
                    </properties> -->
                    <pingURL>http://localhost:8080/example-war</pingURL>
                  </deployable>
                </deployables>
              </deployer>
            </configuration>
          </execution>
          <execution>
            <id>stop-container</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>
于 2008-10-06T21:48:39.947 に答える
2

私は現在、簡単に「劣化」して単純な外部タスク実行者になる可能性のある、より具体的なプラグインに取り組んでいますが、他にも考慮すべきことがかなりあります。

  1. プロセスが実際に開始されたことをどうやって知ることができますか?
  2. リターンコードはどうする?
  3. エグゼキュータ プラグインが最初に実行されるようにするにはどうすればよいですか (テスト コンパイル フェーズにバインドします)。

実際にプラグインの開発を始めればもっとあると思いますが、汎用の実行プログラムは本当に必要なのでしょうか?

アップデート:

あると思います... CodeHaus には優れた Maven プラグインのセットがあります。必要なものは次のとおりです: http://mojohaus.org/exec-maven-plugin/

于 2008-10-06T19:24:48.427 に答える
1

おそらく、実際の統合テストを maven ライフサイクルの統合テスト フェーズにバインドする必要があります。実際のテストを行うためにフェールセーフ (適切な名前のフェイルセーフ プラグインなど) のプラグインを使用する場合は、次のようにフェーズを実行できます。

pre-integration-test : 外部アプリケーションを開始します (exec プラグインまたはここでの他の提案のいずれかを使用)

integration-test : フェイルセーフ プラグインを使用して実際の統合テストを実行します

post-integration-test : 外部アプリケーションをシャットダウンし、その他の必要なクリーンアップを行います

verify : フェイルセーフ プラグインにテストの結果を検証させ、この時点でビルドを失敗させます

exec プラグインを使用するのはかなり簡単です。秘訣は、アプリケーションをバックグラウンドで起動することです。次のフェーズでテストを開始する前に、アプリが完全に起動していることを確認するように注意する必要があります。残念ながら、バックグラウンドでの実行中にアプリケーションを起動して十分に動作していることを確認することは、必ずしも簡単な作業ではなく、その方法の詳細はアプリケーションによって異なります。多くの場合、アプリケーションにカスタム コードが含まれます。

于 2011-08-23T17:10:19.703 に答える
0

アプリケーションサーバーを起動しますか?CargoとそのMavenプラグインをご覧ください。

于 2008-10-06T21:40:00.840 に答える