JUnit4 → JUnit5 migrate チートシート


依存の変更

- testImplementation("junit:junit:4.xx.xx")
+ testImplementation("org.junit.jupiter:junit-jupiter:5.xx.xx")
+ testRuntimeOnly("org.junit.platform:junit-platform-launcher")

JUnit4 と JUnit5 を共存させる場合は以下を追加

+ testImplementation("org.junit.vintage:junit-vintage-engine:5.xx.xx")


Maven の場合は以下。

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.xx.xx</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <version>5.xx.xx</version>
      <scope>test</scope>
  </dependency>

maven-surefire-plugin は 2.22 以降が必要。

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
          <trimStackTrace>false</trimStackTrace>
        </configuration>
      </plugin>
    </plugins>
  </build>


インポート宣言の置換で完結するもの

- import org.junit.Test;
+ import org.junit.jupiter.api.Test;
- import static org.junit.Assert.fail;
+ import static org.junit.jupiter.api.Assertions.fail;
- import static org.junit.Assert.assertThat;
+ import static org.hamcrest.MatcherAssert.assertThat;
- import static org.junit.Assert.assertTrue;
- import static org.junit.Assert.assertFalse;
+ import static org.junit.jupiter.api.Assertions.assertTrue;
+ import static org.junit.jupiter.api.Assertions.assertFalse;

ただし、assertTrueassertFalse は message 引数の位置が逆転している。メッセージを指定しているものはコードの変更も必要。

- assertFalse(String message, boolean condition)
+ assertFalse(boolean condition, String message)


クラスの置き換えで済むもの

- import org.junit.Before;
- import org.junit.After;
- @Before
- @After

+ import org.junit.jupiter.api.BeforeEach;
+ import org.junit.jupiter.api.AfterEach;
+ @BeforeEach
+ @AfterEach
- import org.junit.BeforeClass;
- import org.junit.AfterClass;
- @BeforeClass
- @AfterClass

+ import org.junit.jupiter.api.BeforeAll;
+ import org.junit.jupiter.api.AfterAll;
+ @BeforeAll
+ @AfterAll
- import org.junit.Ignore;
- @Ignore

+ import org.junit.jupiter.api.Disabled;
+ @Disabled


expected から assertThrows への書き換え

- @Test(expected = FooException.class)  
- public void testfoo() {
-  bar();
- }

+ import static org.junit.jupiter.api.Assertions.assertThrows;
+ @Test
+ public void testfoo() {
+     assertThrows(FooException.class, () -> bar());
+ }


timeout から assertTimeout への書き換え

- @Test(timeout = 1)

+ Assertions.assertTimeout(Duration.ofMillis(1), () -> foo());


@RunWith から @ExtendWith への変更

RunWithExtendWith に変更。

mockito 利用時にはtestImplementation("org.mockito:mockito-junit-jupiter:5.x.x") の依存を追加して以下の変更が必要。

- import org.junit.runner.RunWith;
- import org.mockito.junit.MockitoJUnitRunner;
- 
- @RunWith(MockitoJUnitRunner.class)  
- public class FooTest { }


+ import org.junit.jupiter.api.extension.ExtendWith;
+ import org.mockito.junit.jupiter.MockitoExtension;
+ 
+ @ExtendWith(MockitoExtension.class)
+ public class FooTest { }

@RunWith(SpringRunner::class) も同じように @ExtendWith(SpringExtension.class) に変更。


その他必要な書き換え

@Category@Tag に置き換えられた。

- import org.junit.experimental.categories.Category;
- @Category(SlowTests.class)

+ import org.junit.jupiter.api.Tag;
+ @Tag("slow")


@Rule@ClassRule は削除された。@ExtendWith や @RegisterExtension への書き換えが必要。

@Rule を継続して利用する場合は、JUnit5 における限定的なサポートが提供されている。

+ testImplementation("org.junit.jupiter:junit-jupiter-migrationsupport:5.xx.xx")
@EnableRuleMigrationSupport    // @Rule only
@EnableJUnit4MigrationSupport  // @Rule and @Ignore
class FooTest { }



OpenRewrite によるJUnit4 → JUnit5マイグレーション

OpenRewrite プラグイン導入

plugins {
  id("org.openrewrite.rewrite") version("6.29.0")  
}  
  
rewrite {  
  activeRecipe("org.openrewrite.java.testing.junit5.JUnit5BestPractices")  
}  
  
dependencies {  
  rewrite(platform("org.openrewrite.recipe:rewrite-recipe-bom:latest.release"))  
  rewrite("org.openrewrite.recipe:rewrite-testing-frameworks")  
}

ソース書き換え

./gradlew rewriteRun

OpenRewrite によるHamcrest → AssertJマイグレーション

assertj-core の依存追加

testImplementation("org.assertj:assertj-core:3.27.0")

OpenRewrite プラグイン導入

plugins {
    id("org.openrewrite.rewrite") version "6.29.0"
}
rewrite {  
    activeRecipe("org.openrewrite.java.testing.assertj.Assertj")  
}
dependencies {
    rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:2.24.0")
}

ソース書き換え

./gradlew rewriteRun