LoginSignup
8
13

More than 5 years have passed since last update.

JUnit 4からJUnit 5に移行する

Posted at

JUnit 5の機能を利用するために、既存のJUnit 4プロジェクトの移行を試しました。

移行手順

(1) 依存関係を追加する

以下の3つの依存関係を追加する。

  • junit-jupiter-engine
  • junit-platform-launcher
  • junit-vintage-engine(JUnit 3・JUnit 4のテストを実行する場合のみ必要)

pom.xml記述例(2018年11月28日時点の最新版)

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.3.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
</dependency>
(2) テストメソッドを更新する

junit-vintage-engineを使えばJUnit 5でJUnit 3・JUnit 4テストを実行できるが、単純なアノテーションの置換で変換できるものは更新してしまうこともできる。

JUnit 4 (org.junit.*) JUnit 5 (org.junit.jupiter.api.*)
Test Test ※
Ignore Disabled
Before BeforeEach
After AfterEach
BeforeClass BeforeAll
AfterClass AfterAll

※ アノテーションの属性は互換性がないことに注意

EclipseでJUnit 5実行する

Eclipse 4.7.3 (Oxygen) 以上であればJUnit 5を実行できる。実行するにはテストしたいクラス(メソッド)を右クリック → 実行 → JUnitテスト を選択すればよい。

※ Eclipseはプロジェクトの依存関係から実行するJUnitのバージョンを自動的に選択する。(JUnit 4の古い実行構成が残っている場合はこれを削除してから実行すること。)

参考

8
13
0

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