TestWatcher
=> 以下のイベント時に呼び出されるメソッドを提供する * starting : テスト実行前 * succeeded : テスト実行後(成功時) * failed : テスト実行後(失敗時) * finished : テスト実行後(成功/失敗に関わらず)
使いどころ
例えば... * テスト失敗時にログを出力する(以下の関連記事を参照のこと)http://blogs.yahoo.co.jp/dk521123/35180911.html
など...
サンプル
DemoTestWatcher.java
* TestWatcherを継承import org.junit.internal.AssumptionViolatedException; import org.junit.rules.TestWatcher; import org.junit.runner.Description; public class DemoTestWatcher extends TestWatcher { @Override protected void starting(Description description) { System.out.println("starting() getClassName : " + description.getClassName()); System.out.println("starting() getMethodName : " + description.getMethodName()); System.out.println("starting() getDisplayName : " + description.getDisplayName()); } @Override protected void skipped(AssumptionViolatedException exception, Description description) { System.out.println("skipped() getClassName : " + description.getClassName()); System.out.println("skipped() getMethodName : " + description.getMethodName()); System.out.println("skipped() getDisplayName : " + description.getDisplayName()); System.out.println(exception.toString()); } @Override protected void succeeded(Description description) { System.out.println("succeeded() getClassName : " + description.getClassName()); System.out.println("succeeded() getMethodName : " + description.getMethodName()); System.out.println("succeeded() getDisplayName : " + description.getDisplayName()); } @Override protected void failed(Throwable throwable, Description description) { System.out.println("failed() getClassName : " + description.getClassName()); System.out.println("failed() getMethodName : " + description.getMethodName()); System.out.println("failed() getDisplayName : " + description.getDisplayName()); System.out.println(throwable.toString()); } @Override protected void finished(Description description) { System.out.println("finished() getClassName : " + description.getClassName()); System.out.println("finished() getMethodName : " + description.getMethodName()); System.out.println("finished() getDisplayName : " + description.getDisplayName()); } }
DemoJUnitTest.java
* JUnitimport static org.junit.Assert.*; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; public class DemoJUnitTest { // ★!!ここがポイント!!★ @Rule public DemoTestWatcher watchman = new DemoTestWatcher(); @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("setUpBeforeClass()"); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("tearDownAfterClass()"); } @Before public void setUp() throws Exception { System.out.println("setUp()"); } @After public void tearDown() throws Exception { System.out.println("tearDown()"); } @Test public void testAndFail() { System.out.println("testAndFail()"); fail("This test is fail."); } @Test public void testAndSucceed() { System.out.println("testAndSucceed()"); } @Test public void testPreconditionViolation() { System.out.println("testPreconditionViolation()"); assertTrue(false); } }
出力結果
setUpBeforeClass() starting() getClassName : com.sample.testWatcher.DemoJUnitTest starting() getMethodName : testAndSucceed starting() getDisplayName : testAndSucceed(com.sample.testWatcher.DemoJUnitTest) setUp() testAndSucceed() tearDown() succeeded() getClassName : com.sample.testWatcher.DemoJUnitTest succeeded() getMethodName : testAndSucceed succeeded() getDisplayName : testAndSucceed(com.sample.testWatcher.DemoJUnitTest) finished() getClassName : com.sample.testWatcher.DemoJUnitTest finished() getMethodName : testAndSucceed finished() getDisplayName : testAndSucceed(com.sample.testWatcher.DemoJUnitTest) starting() getClassName : com.sample.testWatcher.DemoJUnitTest starting() getMethodName : testAndFail starting() getDisplayName : testAndFail(com.sample.testWatcher.DemoJUnitTest) setUp() testAndFail() tearDown() failed() getClassName : com.sample.testWatcher.DemoJUnitTest failed() getMethodName : testAndFail failed() getDisplayName : testAndFail(com.sample.testWatcher.DemoJUnitTest) java.lang.AssertionError: This test is fail. finished() getClassName : com.sample.testWatcher.DemoJUnitTest finished() getMethodName : testAndFail finished() getDisplayName : testAndFail(com.sample.testWatcher.DemoJUnitTest) starting() getClassName : com.sample.testWatcher.DemoJUnitTest starting() getMethodName : testPreconditionViolation starting() getDisplayName : testPreconditionViolation(com.sample.testWatcher.DemoJUnitTest) setUp() testPreconditionViolation() tearDown() failed() getClassName : com.sample.testWatcher.DemoJUnitTest failed() getMethodName : testPreconditionViolation failed() getDisplayName : testPreconditionViolation(com.sample.testWatcher.DemoJUnitTest) java.lang.AssertionError finished() getClassName : com.sample.testWatcher.DemoJUnitTest finished() getMethodName : testPreconditionViolation finished() getDisplayName : testPreconditionViolation(com.sample.testWatcher.DemoJUnitTest) tearDownAfterClass()
参考文献
http://www7b.biglobe.ne.jp/~archer/tryjunit4/tryjunit4Rules.htmlhttp://qiita.com/daisy1754/items/6d692bc5d39d40e498fb
http://irof.hateblo.jp/entry/20111216/p1