2013-02-08 16 views
5

私は、テストグループ実行の全期間中実行したいグリズリーHttpServerを持っています。さらに、テスト内の@RuleのグローバルHttpServerインスタンスと対話したいと思います。確実に実行されるすべてのテストの前後のコード実行

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

今のところ、私は静的フィールドを怠惰に初期化し、サーバーをRuntime.addShutdownHook()から停止していると思います。

+0

からである。次に、確実な中で、あなたが使用して、独自のリスナーを指定することができますか? – TheWhiteRabbit

+0

POJOまたはTestNGテストを使用している場合は、@BeforeClassを使用することができます – TheWhiteRabbit

+0

@TechExchange Maven surefireを使用していることを明確にするための質問を更新しました – hertzsprung

答えて

7

メイヴァンソリューションと確実なソリューションの2つのオプションがあります。最も結合されていない解決策は、pre-integration-testpost-integration-testフェーズでプラグインを実行することです。 Introduction to the Build Lifecycle - Lifecycle Referenceを参照してください。私はグリズリーに慣れていないんだけど、ここでは例として使用して桟橋です:startための位相はpre-integration-teststoppost-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> 

注意。グリズリーメイヴンプラグインがあるかどうかはわかりませんが、代わりにmaven-antrun-pluginを使用できます。

2番目のオプションは、JUnit RunListenerを使用することです。 RunListenerだから、あなたがRunStartedとRunFinishedのために聞くことができなど

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 {} 
} 

試験開始、テスト終了、テストの失敗、テストの成功などのイベントを、テストするためにリッスンします。これらは、あなたが望むサービスを開始/停止します。

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

をJUnitのない場合、これはあなたがいずれかを使用しているMaven Surefire Plugin, Using JUnit, Using custom listeners and reporters

+0

'HttpServer'インスタンスにアクセスする必要があるため、 'TestRule'からは' RunListener'が有望ですね、ありがとう! – hertzsprung

+0

私のために、統合前段階の一環として、桟橋サーバーが始動します。最後のログ行は次のとおりです:[INFO] Jetty Serverを開始しました。その後、何も起こりません。それは立ち往生する。フェイルセーフフェールセーフプラグインはテストを実行せず、突堤停止サーバーも停止しません。どのようなアイデアが間違っている?私はあなたが指定したものと同じ設定を使用しています。 –

関連する問題