In this tutorial, we'll use OpenRewrite to perform an automated migration from the venerable JUnit 4 testing framework to its successor JUnit 5. JUnit is a popular tool that many other libraries and frameworks interact with. OpenRewrite supports some of the popular integrations, such as Mockito and Spring-Boot, out of the box.
plugins { id("java") id("org.openrewrite.rewrite") version("6.10.1") } rewrite { activeRecipe("org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration") } repositories { mavenCentral() // rewrite-spring is published to Maven Central } dependencies { rewrite(platform("org.openrewrite.recipe:rewrite-recipe-bom:2.8.1")) rewrite("org.openrewrite.recipe:rewrite-spring")// Other project dependencies }
SpringBoot2JUnit4to5Migration is a superset of the normal JUnit 4 to 5 and Mockito 1 to 3 recipes, with some additional Spring-specific functionality. If you activate this recipe it is not necessary to also activate the base JUnit or Mockito migration recipes.
plugins { id("java") id("org.openrewrite.rewrite") version("6.10.1") } rewrite { activeRecipe("org.openrewrite.java.testing.junit5.JUnit5BestPractices") } repositories { mavenCentral() // rewrite-testing-frameworks is published to Maven Central } dependencies { implementation(platform("org.openrewrite.recipe:rewrite-recipe-bom:2.8.1")) rewrite("org.openrewrite.recipe:rewrite-testing-frameworks")// Other project dependencies }
At this point, you're ready to execute the migration by running mvn rewrite:run or gradlew rewriteRun. After running the migration you can inspect the results with git diff (or equivalent), manually fix anything that wasn't able to be migrated automatically, and commit the results.
Before and After
For the full list of changes, this recipe will make, see its reference page.
packageorg.openrewrite.example;importorg.junit.AfterClass;importorg.junit.Assert;importorg.junit.Before;importorg.junit.BeforeClass;importorg.junit.Test;importorg.junit.Test;importorg.junit.Rule;importorg.junit.rules.ExpectedException;importorg.junit.rules.TemporaryFolder;importorg.junit.rules.Timeout;importjava.io.File;importjava.io.IOException;importjava.util.List;publicclassExampleJunitTestClass { @RulepublicTemporaryFolder folder =newTemporaryFolder(); @Beforepublicvoidbefore() { } @AfterClasspublicstaticvoidafterClass() { } @Test(expected =RuntimeException.class)publicvoidfoo() throwsIOException {File tempFile =folder.newFile();File tempFile2 =folder.newFile("filename");File tempDir =folder.getRoot();File tempDir2 =folder.newFolder("parent","child");File tempDir3 =folder.newFolder("subdir");File tempDir4 =folder.newFolder();String foo ="foo";thrownewRuntimeException(foo); } @Test(expected =IndexOutOfBoundsException.class)publicvoidfoo2() {int arr =newint[]{}[0]; } @RulepublicExpectedException throwz =ExpectedException.none(); @Testpublicvoidfoo3() {throwz.expect(RuntimeException.class);thrownewRuntimeException(); } @TestpublicvoidassertsStuff() {Assert.assertEquals("One is one",1,1);Assert.assertArrayEquals("Empty is empty",newint[]{},newint[]{});Assert.assertNotEquals("one is not two",1,2);Assert.assertFalse("false is false",false);Assert.assertTrue("true is true",true);Assert.assertEquals("foo is foo","foo","foo");Assert.assertNull("null is null",null);Assert.fail("fail"); } @Test(timeout =500)publicvoidbar() { }}
Dependency management for Gradle is not currently available but this feature is on OpenRewrite's roadmap.
Known Limitations
Not every JUnit 4 feature or library has a direct JUnit 5 equivalent. In these cases, manual changes will be required after the automation has run. This list is not exhaustive. See the rewrite-testing-frameworks issue tracker.
The JUnit5 equivalent to JUnit4 ClassPathSuite is not yet released
org.junit.ComparisonFailure
org.junit.MethodRule
TestRule, TestWatcher, and Description
Your codebase may also have custom JUnit 4 Rules or Runners that will not be migrated automatically by our recipes. If your codebase has a lot of customized JUnit 4 extensions, consider writing your own recipe to handle those and running it alongside this migration.
If you discover a shortcoming of this migration that should be covered, file an issue or submit a pull request on the rewrite-testing-frameworks github project.
See how this recipe works across multiple open-source repositories
The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.
Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.