2016-11-18 6 views
1

org.junit.runner.Runnerを実装して私自身のJUnit-Runnerを作ったので、@RunWith -Annotationを使ってUnitTestsを実行することができます。それはこのようにややlookesJUnit-Runnerを正しくシャットダウンする方法は?

:リソースをクリーンアップするには

public class MyRunner extends Runner { 

    private Context myContext; 
    myContext.init(); 
    private final BlockJUnit4ClassRunner runner; 

    public MyRunner(final Class<?> clazz) throws InitializationError { 
     myContext = new Context(); 

     runner = new BlockJUnit4ClassRunner(clazz); 
    } 

    @Override 
    public void run(final RunNotifier notifier) { 
     runner.run(notifier); 
    } 

    @Override 
    public Description getDescription() { 
     return runner.getDescription(); 
    } 

    public void filter(final Filter filter) throws NoTestsRemainException { 
     runner.filter(filter); 
    } 
} 

、私はMyContext.close()を呼び出すことによってMyContextをシャットダウンする必要があります。テストを実行した後、私のリソースがきれいになるように、どこで呼び出すべきですか?

答えて

-1

のテストが実行された後、私のリソースがクリーンアップされるように、どこから呼び出すべきですか?私の答えを更新し

、以下に示すように、あなたがorg.junit.runner.notification.RunListenerを使用することができます。

を(1)独自のRunListenerクラスを作成します。

public class MyRunnerListener extends RunListener { 
    private Context context; 

    public MyRunnerListener(Context context) { 
     this.context = context; 
    } 

    void testRunFinished(Result result) { 
      context.close(); 
    } 
} 

(2)MyRunnerListener内部を使用してくださいMyRunner:

public class MyRunner extends Runner { 

    private Context myContext; 
    MyRunnerListener runnerListener; 
    private final BlockJUnit4ClassRunner runner; 

    public MyRunner(final Class<?> clazz) throws InitializationError { 
     myContext = new Context(); 
     myContext.init(); 
     runnerListener = new MyRunnerListener(myContext); 

     runner = new BlockJUnit4ClassRunner(clazz); 
    } 

    @Override 
    public void run(final RunNotifier notifier) { 
     notifier.addListener(runnerListener); 
     runner.run(notifier); 
    } 

    @Override 
    public Description getDescription() { 
     return runner.getDescription(); 
    } 

    public void filter(final Filter filter) throws NoTestsRemainException { 
     runner.filter(filter); 
    } 
} 

P.S .: Runnerを使用しない場合は、Markusの回答に従うことができます(TestRuleTestRunnerは使用しません)。

+0

...あなたが将来的に出くわすかもしれランダムtestframeworkによって導入することができる任意のランナーと一緒に使用することができ

テストはしますが、ランナーはテストしません。 – Bob

+0

答えを更新しました。それを見てください。 – developer

+0

ランナーが複数のテストクラスで使用される可能性があるため、動作しません。 – Bob

1

私はあなたがachiveしようとしているかわからないんだけど、あなたはすでにJUnit's Rulesを見て持っていましたか?

public class MyContextRule extends ExternalResource { 

    private final Context myContext; 

    public MyContextRule() { 
     myContext = new Context(); 
    } 

    @Override 
    protected void before() throws Throwable { 
     myContext.init(); 
    } 

    @Override 
    protected void after() { 
     myContext.close(); 
    } 

} 

使用法:Runner秒以上

public class MyTest { 
    @ClassRule 
    public static MyContextRule contextRule = new MyContextRule(); 

    //... 
} 

JUnitのRuleの利点は、あなたが唯一の走者を持つことができますしながら、あなたは、それらの複数を持つことができるということです。これは下のクラスに適用される質問に答えていないので、カスタムRule

関連する問題