Table of Contents
JUnit 5 のユーザーズガイドを読みました。 公式ドキュメントのうち、重要そうなところをピックアップしてまとめています。 そのため公式ドキュメントにあってこちらにない文章もあります。
この記事は からの続きです。
3 テストを記述する
3.1 アノテーション
JUnit Jupiter では次のアノテーションを使って、 テストの設定や、フレームワークの拡張ができます。
すべてのコアアノテーションは junit-jupiter-api
の org.junit.jupiter.api
パッケージ にあります。
アノテーション | 説明 |
---|---|
@Test |
関数がテスト用の関数であることを示します。 JUnit 4 の @Test アノテーション とは異なり、 this annotation does not declare any attributes, since test extensions in JUnit Jupiter operate based on their own dedicated annotations. オーバライドされない限り Such methods are inherited unless they are overridden. |
@ParameterizedTest |
メソッドがパラメタライズドテストであることを示します。 Such methods are inherited unless they are overridden. |
@RepeatedTest |
メソッドが Denotes that a method is a test template for a repeated test. Such methods are inherited unless they are overridden. |
@TestFactory |
Denotes that a method is a test factory for dynamic tests. Such methods are inherited unless they are overridden. |
@TestInstance |
Used to configure the test instance lifecycle for the annotated test class. Such annotations are inherited. |
@TestTemplate |
Denotes that a method is a template for test cases designed to be invoked multiple times depending on the number of invocation contexts returned by the registered providers. Such methods are inherited unless they are overridden. |
@DisplayName |
テストクラスおよびテストメソッドに、独自の表示名を設定します。 Such annotations are not inherited. |
@BeforeEach |
このアノテーションが付与されたメソッドは @Test または @RepeatedTest , @ParameterizedTest , @TestFactory でアノテートされたそれぞれのメソッドが実行される前に実行されます。 JUnit 4 での @Before に相当します。 Such methods are inherited unless they are overridden. |
@AfterEach |
このアノテーションが付与されたメソッドは @Test または @ParameterizedTest , @TestFactory でアノテートされたそれぞれのメソッドが実行された後に実行されます。 JUnit 4 での @After に相当します。 Such methods are inherited unless they are overridden. |
@BeforeAll |
Denotes that the annotated method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @BeforeClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the “per-class” test instance lifecycle is used). |
@AfterAll |
Denotes that the annotated method should be executed after all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @AfterClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the “per-class” test instance lifecycle is used). |
@Nested |
Denotes that the annotated class is a nested, non-static test class. @BeforeAll and @AfterAll methods cannot be used directly in a @Nested test class unless the “per-class” test instance lifecycle is used. Such annotations are not inherited. |
@Tag |
Used to declare tags for filtering tests, either at the class or method level; JUnit 4 でいうところの TestNG , Categories に相当します。 analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are inherited at the class level but not at the method level.
|
@Disabled |
テストクラス、テストメソッドを無効にする場合に使います。 JUnit 4 でいうところの @Ignore に相当します。 Such annotations are not inherited. |
@ExtendWith |
カスタム拡張を登録する場合に使います。 Used to register custom extensions. Such annotations are inherited. |
3.1.1 メタアノテーションと合成アノテーション
JUnit Jupiter のアノテーションはメタアノテーションとして扱われるため、 JUnit Jupiter アノテーション のセマンティクスを継承した合成アノテーションを作成できます。
例えば @Tag("fast")
をコピー&ペーストする代わりに、 次のように独自の合成アノテーション @Fast
を作成して利用できます。
3.2 テストクラスとメソッド
テストメソッド は次のアノテーションで直接またはメタアノテートされたインスタンスメソッドです。
@Test
@RepeatedTest
@ParameterizedTest
@TestFactory
@TestTemplate
トップレベルのクラスまたは静的メンバクラスで、少なくともひとつのテストメソッドを持っているものをテストクラスといいます。
テストクラスもテストメソッドも、 public
にする必要はありません。
3.3 表示名
テストクラスもテストメソッドも、独自に表示名を決めることができます。 スペース、特殊文字、絵文字も使用可能です。 その名前はテストランナーおよびテストレポートで表示されます。
IntelliJ IDEA のツールウィンドウでも表示されます。

ただ、 Gradle のコマンド ./gradlew test
でテストを実行した場合、 Gradle の出力するレポートで絵文字は ?? として表示されます。

3.4 アサーション
JUnit Jupiter のアサーションには、 JUnit 4 にあったアサーションの多くと、 Java 8 のラムダと連携可能ないくつかのアサーションがあります。 すべてのアサーションは org.junit.jupiter.api.Assertions
クラス の静的メソッドです。
JUnit Jupiter は Kotlin とうまく連携するアサーションも備えています。 すべての JUnit Jupiter Kotlin アサーション は org.junit.jupiter.api
パッケージ のトップレベル関数です。 たとえば assertAll
は、 Java のコードでは org.junit.jupiter.api.Assertions.assertAll
を使っていましたが、 Kotlin では Kotlin で使いやすいようにカスタマイズされている org.junit.jupiter.api.assertAll
を使います。 もちろん、 org.junit.jupiter.api.Assertions.assertAll
も利用可能です。 (関連: Qiita記事: JUnit 5 assertAll)
次のサンプルコードで、下の2つのテストメソッドは、 Kotlin のサンプルにのみあるテストメソッドです。