Upgrade to Pro — share decks privately, control downloads, hide ads and more …

これで失敗しない! JUnit 5 へのマイグレーション方法

akkie76
July 18, 2022
450

これで失敗しない! JUnit 5 へのマイグレーション方法

akkie76

July 18, 2022
Tweet

Transcript

  1. 自己紹介 Akihiko Sato / @akkiee76 株式会社ラクス 楽楽精算開発 モバイル開発 / Lead

    Engineer SaaS (Backend, Frontend) / Mobile (iOS, Android) 主な業務は、設計 〜 受入、チームの課題改善など マイブームは、縄跳び・腹筋ローラー Twitter QR
  2. 1. package (import) の変更 JUnit 4 では org.junit 配下の package

    にあったクラス群が JUnit 5 では org.junit.jupiter.api 配下となるため、 import の修正が必要になります。 対象 JUnit 4 JUnit 5 @Test など org.junit.* org.junit.jupiter.api.* Assertion など org.junit.Assert.* org.junit.jupiter.api.Assertions.*
  3. 2. アノテーションの置換 具体的に置換が必要なアノテーション(一部) JUnit 4 JUnit 5 @Before @BeforeEach @After

    @AfterEach @BeforeClass @BeforeAll @AfterClass @AfterAll @Ignore @Disabled @Category @Tag https://blog.jetbrains.com/idea/2020/08/migrating-from-junit-4-to-junit-5/
  4. 3. テスト階層化方法の変更 JUnit 5 からは @Nested が利用可能になるため、 テスト階層の記述が容易になります。 @RunWith(Enclosed.class) public

    class SampleJUnit4Test { public static class RelationalTest1 { @Test public void test() { ... } } public static class RelationalTest2 { @Test public void test() { ... } } } public class SampleJUnit5Test { @Nested class RelationalTest1 { @Test public void test() { ... } } @Nested class RelationalTest2 { @Test public void test() { ... } } } JUnit 4 JUnit 5
  5. 4. Assert テストの変更 Assert の記述も変更になったため、修正が必要になります。 JUnit 4 @Test(expected = NullPointerException.class)

    public void testNullPointerException() { /* NullPointerException が発生するコード */ } @Test public void testNullPointerException() { assertThrows(NullPointerException.class, () -> { /* NullPointerException が発生するコード */ }); } JUnit 5
  6. 4. Assert テストの変更 JUnit 4 @Rule public ExpectedException expectedException =

    ExpectedException.none(); @Test public void testExpectedException() { expectedException.expect(HogeException.class); expectedException.expectMessage("HogeException message"); /* HogeException が発生するコード */ } JUnit 5 @Test public void testExpectedException() { HogeException exception = assertThrows(HogeException.class, () -> { /* HogeException が発生するテストコード */ }); assertEquals("HogeException message", exception.getMessage()); }
  7. マイグレーション対応中の課題 3.1. Running JUnit 4 Tests on the JUnit Platform

    Just make sure that the junit-vintage-engine artifact is in your test runtime path. In that case JUnit 3 and JUnit 4 tests will automatically be picked up by the JUnit Platform launcher. 公式ガイドラインを調べてみると、以下の記載を発見する。 https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4 junit-vintage-engine を使うことで JUnit 4 を維持しながら、 マイグレーションすることが可能ということが分かりました。
  8. マイグレーション対応中の課題が解決 junit-vintage-engine を利用し、以下の方針で進めることに。   1. junit-vintage-engine を導入   2. import

    と annotation の修正   3. テスト階層化方法の修正   4. Assert の修正   5. junit-vintage-engine を削除
  9. マイグレーション対応方針 とはいえ、対象ファイルが多いのでスコープを適度に細分化   2. import と annotation の修正(ボリューム大) ・各 package

    ごと 5 ファイル程度に分けて MR を対応   3. テスト階層化方法の修正   ・適度な粒度の MR に分割   4. Assert の修正   ・適度な粒度の MR に分割
  10. まとめ JUnit 5 へマイグレーションのポイント 👉  junit-vintage-engine を導入して JUnit 4 と共存させ

    CI を維持する 👉  import 、アノテーション修正はスコープを分けて対応する   (修正のスコープはなるべく小さくする) 👉 修正ファイルが多い場合は、一覧化して進捗管理する