Migrate to JUnit 5 from JUnit 4
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.

Example Configuration

If your project is a Spring or Spring-Boot project take a dependency on rewrite-spring and activate the SpringBoot2JUnit4to5Migration recipe:
Maven
Gradle
pom.xml
1
<plugin>
2
<groupId>org.openrewrite.maven</groupId>
3
<artifactId>rewrite-maven-plugin</artifactId>
4
<version>4.13.0</version>
5
<configuration>
6
<activeRecipes>
7
<recipe>org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration</recipe>
8
</activeRecipes>
9
</configuration>
10
<dependencies>
11
<dependency>
12
<groupId>org.openrewrite.recipe</groupId>
13
<artifactId>rewrite-spring</artifactId>
14
<version>4.13.0</version>
15
</dependency>
16
</dependencies>
17
</plugin>
Copied!
build.gradle
1
plugins {
2
id("java")
3
id("org.openrewrite.rewrite") version("5.12.0")
4
}
5
6
rewrite {
7
activeRecipe("org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration")
8
}
9
10
repositories {
11
mavenCentral() // rewrite-spring is published to Maven Central
12
}
13
14
dependencies {
15
rewrite("org.openrewrite.recipe:rewrite-spring:4.13.0")
16
17
// Other project dependencies
18
}
Copied!
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.
If your project is not a Spring or Spring-Boot project take a dependency on rewrite-testing-frameworks and activate the JUnit5BestPractices recipe:
Maven
Gradle
pom.xml
1
<plugin>
2
<groupId>org.openrewrite.maven</groupId>
3
<artifactId>rewrite-maven-plugin</artifactId>
4
<version>4.13.0</version>
5
<configuration>
6
<activeRecipes>
7
<recipe>org.openrewrite.java.testing.junit5.JUnit5BestPractices</recipe>
8
</activeRecipes>
9
</configuration>
10
<dependencies>
11
<dependency>
12
<groupId>org.openrewrite.recipe</groupId>
13
<artifactId>rewrite-testing-frameworks</artifactId>
14
<version>1.14.0</version>
15
</dependency>
16
</dependencies>
17
</plugin>
Copied!
build.gradle
1
plugins {
2
id("java")
3
id("org.openrewrite.rewrite") version("5.12.0")
4
}
5
6
rewrite {
7
activeRecipe("org.openrewrite.java.testing.junit5.JUnit5BestPractices")
8
}
9
10
repositories {
11
mavenCentral() // rewrite-testing-frameworks is published to Maven Central
12
}
13
14
dependencies {
15
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:1.14.0")
16
17
// Other project dependencies
18
}
Copied!
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.
JUnit 4 Test Class (Before)
JUnit 5 Test Class (After)
1
package org.openrewrite.example;
2
3
import org.junit.AfterClass;
4
import org.junit.Assert;
5
import org.junit.Before;
6
import org.junit.BeforeClass;
7
import org.junit.Test;
8
import org.junit.Test;
9
import org.junit.Rule;
10
import org.junit.rules.ExpectedException;
11
import org.junit.rules.TemporaryFolder;
12
import org.junit.rules.Timeout;
13
14
import java.io.File;
15
import java.io.IOException;
16
import java.util.List;
17
18
public class ExampleJunitTestClass {
19
20
@Rule
21
public TemporaryFolder folder = new TemporaryFolder();
22
23
@Before
24
public void before() {
25
}
26
27
@AfterClass
28
public static void afterClass() {
29
}
30
31
@Test(expected = RuntimeException.class)
32
public void foo() throws IOException {
33
File tempFile = folder.newFile();
34
File tempFile2 = folder.newFile("filename");
35
File tempDir = folder.getRoot();
36
File tempDir2 = folder.newFolder("parent", "child");
37
File tempDir3 = folder.newFolder("subdir");
38
File tempDir4 = folder.newFolder();
39
String foo = "foo";
40
throw new RuntimeException(foo);
41
}
42
43
@Test(expected = IndexOutOfBoundsException.class)
44
public void foo2() {
45
int arr = new int[]{}[0];
46
}
47
48
@Rule
49
public ExpectedException throwz = ExpectedException.none();
50
51
@Test
52
public void foo3() {
53
throwz.expect(RuntimeException.class);
54
throw new RuntimeException();
55
}
56
57
@Test
58
public void assertsStuff() {
59
Assert.assertEquals("One is one", 1, 1);
60
Assert.assertArrayEquals("Empty is empty", new int[]{}, new int[]{});
61
Assert.assertNotEquals("one is not two", 1, 2);
62
Assert.assertFalse("false is false", false);
63
Assert.assertTrue("true is true", true);
64
Assert.assertEquals("foo is foo", "foo", "foo");
65
Assert.assertNull("null is null", null);
66
Assert.fail("fail");
67
}
68
69
@Test(timeout = 500)
70
public void bar() { }
71
}
Copied!
1
package org.openrewrite.example;
2
3
import org.junit.jupiter.api.*;
4
import org.junit.jupiter.api.io.TempDir;
5
6
import java.io.File;
7
import java.io.IOException;
8
import java.nio.file.Files;
9
import java.util.List;
10
11
import static org.junit.jupiter.api.Assertions.assertThrows;
12
13
public class ExampleJunitTestClass {
14
15
@TempDir
16
File folder;
17
18
@BeforeEach
19
void before() {
20
}
21
22
@AfterAll
23
static void afterClass() {
24
}
25
26
@Test
27
void foo() throws IOException {
28
assertThrows(RuntimeException.class, () -> {
29
File tempFile = File.createTempFile("junit", null, folder);
30
File tempFile2 = newFile(folder, "filename");
31
File tempDir = folder;
32
File tempDir2 = newFolder(folder, "parent", "child");
33
File tempDir3 = newFolder(folder, "subdir");
34
File tempDir4 = Files.createTempDirectory(folder.toPath(), "junit").toFile();
35
String foo = "foo";
36
throw new RuntimeException(foo);
37
});
38
}
39
40
@Test
41
void foo2() {
42
assertThrows(IndexOutOfBoundsException.class, () -> {
43
int arr = new int[]{}[0];
44
});
45
}
46
47
@Test
48
void foo3() {
49
assertThrows(RuntimeException.class, () -> {
50
throw new RuntimeException();
51
});
52
}
53
54
@Test
55
void assertsStuff() {
56
Assertions.assertEquals(1, 1, "One is one");
57
Assertions.assertArrayEquals(new int[]{}, new int[]{}, "Empty is empty");
58
Assertions.assertNotEquals(1, 2, "one is not two");
59
Assertions.assertFalse(false, "false is false");
60
Assertions.assertTrue(true, "true is true");
61
Assertions.assertEquals("foo", "foo", "foo is foo");
62
Assertions.assertNull(null, "null is null");
63
Assertions.fail("fail");
64
}
65
66
@Test
67
@Timeout(500)
68
void bar() {
69
}
70
71
private static File newFile(File root, String fileName) throws IOException {
72
File file = new File(root, fileName);
73
file.createNewFile();
74
return file;
75
}
76
77
private static File newFolder(File root, String ... folders) throws IOException {
78
File result = new File(root, String.join("/", folders));
79
if (!result.mkdirs()) {
80
throw new IOException("Couldn't create folders " + root);
81
}
82
return result;
83
}
84
}
Copied!
Maven pom (Before)
Maven pom (After)
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
<modelVersion>4.0.0</modelVersion>
3
4
<groupId>org.openrewrite.example</groupId>
5
<artifactId>integration-testing</artifactId>
6
<version>1.0</version>
7
<name>integration-testing</name>
8
9
<properties>
10
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11
<java.version>1.8</java.version>
12
</properties>
13
14
<dependencies>
15
<dependency>
16
<groupId>junit</groupId>
17
<artifactId>junit</artifactId>
18
<version>4.12</version>
19
<scope>test</scope>
20
</dependency>
21
<dependency>
22
<groupId>com.googlecode.json-simple</groupId>
23
<artifactId>json-simple</artifactId>
24
<version>1.1.1</version>
25
</dependency>
26
</dependencies>
27
</project>
Copied!
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
<modelVersion>4.0.0</modelVersion>
3
4
<groupId>org.openrewrite.example</groupId>
5
<artifactId>integration-testing</artifactId>
6
<version>1.0</version>
7
<name>integration-testing</name>
8
9
<properties>
10
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11
<java.version>1.8</java.version>
12
</properties>
13
14
<dependencies>
15
<dependency>
16
<groupId>com.googlecode.json-simple</groupId>
17
<artifactId>json-simple</artifactId>
18
<version>1.1.1</version>
19
<exclusions>
20
<exclusion>
21
<groupId>junit</groupId>
22
<artifactId>junit</artifactId>
23
</exclusion>
24
</exclusions>
25
</dependency>
26
<dependency>
27
<groupId>org.junit.jupiter</groupId>
28
<artifactId>junit-jupiter-api</artifactId>
29
<version>5.7.1</version>
30
<scope>test</scope>
31
</dependency>
32
<dependency>
33
<groupId>org.junit.jupiter</groupId>
34
<artifactId>junit-jupiter-engine</artifactId>
35
<version>5.7.1</version>
36
<scope>test</scope>
37
</dependency>
38
</dependencies>
39
</project>
Copied!
If you want to know when dependency management for Gradle will be added to rewrite, follow this issue on our 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.
Unsupported Functionality
Powermock has no JUnit 5 equivalent
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.
Last modified 10d ago