21
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JUnit4.4以降で動的に@Ignoreみたいなことをする

21
Posted at

JUnitで@Ignoreをメソッドにつけると、テストは実行されずにスルーされます。
開発中に一時的に無視させておきたい場合なんかに便利ですね。

@Test
@Ignore
public void サンプル() {
    assertTrue(false);
}

こうした「テストを無視する」ようなことを動的に行う場合はorg.junit.Assumeを使います。
データベースや通信のテストのような結合テストを行う場合に便利でしょう。

実装はこんな感じです。(データベースが有効だったらテストを実行するようなイメージ)

@Test
public void サンプル() {
    org.junit.Assume.assumeTrue(db.isValid());
    assertTrue(db.isEmpty());
}

org.junit.Assumeのメソッドはいくつかあるので、色々なチェックができますね。
@Beforeに設定しておいて複数のテストの事前条件チェックもできたりして便利そうです。

Kobito.szzULB.png

21
18
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?