7

HttpServerテストグループの実行中ずっと実行したいグリズリーがあります。@Ruleさらに、テスト自体の内部からグローバルHttpServerインスタンスと対話したいと思います。

JUnitテストスイートではなくMavenSurefireを使用しているため、テストスイート自体で@BeforeClass/を使用することはできません。@AfterClass

今のところ、私が考えることができるのは、静的フィールドを怠惰に初期化し、サーバーを停止することRuntime.addShutdownHook()だけです-良くありません!

4

1 に答える 1

9

MavenソリューションとSurefireソリューションの2つのオプションがあります。最も結合の少ないソリューションは、pre-integration-testandpost-integration-testフェーズでプラグインを実行することです。ビルドライフサイクルの概要-ライフサイクルリファレンスを参照してください。私はグリズリーに精通していませんが、ここに桟橋を使用した例があります:

 <build>
  <plugins>
   <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <configuration>
     <contextPath>/xxx</contextPath>
    </configuration>
    <executions>
     <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals>
       <goal>run</goal>
      </goals>
      <configuration>
      </configuration>
     </execution>
     <execution>
      <id>stop-jetty</id>
      <phase>post-integration-test</phase>
      <goals>
       <goal>stop</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

のフェーズはであることに注意startpre-integration-teststopくださいpost-integration-test。grizzly mavenプラグインがあるかどうかはわかりませんが、代わりにmaven-antrun-pluginを使用できます。

2番目のオプションは、JUnitRunListenerを使用することです。RunListenerテストの開始、終了、テストの失敗、テストの成功などのテストイベントをリッスンします。

public class RunListener {
    public void testRunStarted(Description description) throws Exception {}
    public void testRunFinished(Result result) throws Exception {}
    public void testStarted(Description description) throws Exception {}
    public void testFinished(Description description) throws Exception {}
    public void testFailure(Failure failure) throws Exception {}
    public void testAssumptionFailure(Failure failure) {}
    public void testIgnored(Description description) throws Exception {}
}

したがって、RunStartedとRunFinishedをリッスンできます。これらはあなたが望むサービスを開始/停止します。次に、surefireで、次を使用してカスタムリスナーを指定できます。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.10</version>
  <configuration>
    <properties>
      <property>
        <name>listener</name>
        <value>com.mycompany.MyResultListener,com.mycompany.MyResultListener2</value>
      </property>
    </properties>
  </configuration>
</plugin>

これは、Maven Surefireプラグイン、JUnitの使用、カスタムリスナーとレポーターの使用からのものです。

于 2013-02-08T12:33:29.530 に答える