Skip to content

Instantly share code, notes, and snippets.

@wreulicke
Last active June 10, 2019 11:47
Show Gist options
  • Save wreulicke/1f53fda79a51387c9774d75f009fdf8d to your computer and use it in GitHub Desktop.
Save wreulicke/1f53fda79a51387c9774d75f009fdf8d to your computer and use it in GitHub Desktop.
JUnit5に移行する

JUnit5(junit-jupiter)に移行する

Gradle

testの設定で junit platformを使うように書き換える。 engineを書き換えるなどはお好きなように。

+	test {
+		useJUnitPlatform()
+	}

PMD

PMDのJUnit4向けのfalse positiveで死ぬ

issueとしては以下になる。

6.7.0で対応されているので 6.7.0以降にアップデートしましょう

テストコードの書き換え

色々直さないといけない。

テストコードのsedによる書き換え

当方Macなので、sedがgnu sedじゃないので その辺は読み替えてください。

git ls-files '*.java' | xargs sed -i '' 's/import org.junit.runner.RunWith/import org.junit.jupiter.api.extension.ExtendWith/g'
git ls-files '*.java' | xargs sed -i '' 's/@RunWith(SpringRunner.class)/@ExtendWith(SpringExtension.class)/g'
git ls-files '*.java' | xargs sed -i '' 's/import org.springframework.test.context.junit4.SpringRunner/import org.springframework.test.context.junit.jupiter.SpringExtension/g'
git ls-files '*.java' | xargs sed -i '' 's/@RunWith(MockitoJUnitRunner.class)/@ExtendWith(MockitoExtension.class)/g'
git ls-files '*.java' | xargs sed -i '' 's/import org.mockito.junit.MockitoJUnitRunner/import org.mockito.junit.jupiter.MockitoExtension/g'

git ls-files '*.java' | xargs sed -i '' 's/import org.junit.After/import org.junit.jupiter.api.AfterEach/g'
git ls-files '*.java' | xargs sed -i '' 's/@After/@AfterEach/g'

git ls-files '*.java' | xargs sed -i '' 's/import org.junit.Before/import org.junit.jupiter.api.BeforeEach/g'
git ls-files '*.java' | xargs sed -i '' 's/@Before/@BeforeEach/g'

git ls-files '*.java' | xargs sed -i '' 's/import org.junit.Test/import org.junit.jupiter.api.Test/g'
git ls-files '*.java' | xargs sed -i '' 's/@Test/@Test/g'

git ls-files '*.java' | xargs sed -i '' 's/import org.junit.Ignore/import org.junit.jupiter.api.Disabled/g'
git ls-files '*.java' | xargs sed -i '' 's/@Ignore/@Disabled/g'

WireMockのRule

WireMockのRule使えないの辛い ひとまず、以下の3rd party moduleを使うことにした。

とりあえず今回はInjectServerを使う形で書き換えた。

InjectServer使う場合

- @Rule
- WireMockRule wireMock = new WireMockRule(WireMockConfiguration.wireMockConfig().dynamicPort());
+ @ConfigureWireMock
+ Options options = WireMockConfiguration.wireMockConfig().dynamicPort();
+
+ @InjectServer
+ WireMockServer wireMock;

Managedを使う場合

- @Rule
- WireMockRule wireMock = new WireMockRule(WireMockConfiguration.wireMockConfig().dynamicPort());
+ @Managed
+ WireMockServer wireMock = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());

junitのアサーションを使っている場合

面倒なので、assertjに全部書き換える。 junit-jupiterのassertion APIは順番が入れ替わってたりする。

@Test(expected = HogeException.class)

assertjのassertThatThrownByか assertjのcatchThrowablesを使って 書き換える

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment