org.openrewrite.java.testing.junit5.JUnit4to5Migration
Migrates JUnit 4.x tests to JUnit Jupiter.
- junit
- testing
GitHub, Issue Tracker, Maven Central
- groupId: org.openrewrite.recipe
- artifactId: rewrite-testing-frameworks
- version: 2.0.2
{% tabs %} {% tab title="org/openrewrite/java/testing/junit5/MockitoTests.java" %}
{% code title="org/openrewrite/java/testing/junit5/MockitoTests.java" %}
package org.openrewrite.java.testing.junit5;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.List;
import static org.mockito.Mockito.verify;
public class MockitoTests {
@Mock
List<String> mockedList;
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void usingAnnotationBasedMock() {
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
{% endcode %}
{% code title="org/openrewrite/java/testing/junit5/MockitoTests.java" %}
package org.openrewrite.java.testing.junit5;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.List;
import static org.mockito.Mockito.verify;
public class MockitoTests {
@Mock
List<String> mockedList;
@BeforeEach
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
void usingAnnotationBasedMock() {
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
{% endcode %}
{% endtab %} {% tab title="Diff" %} {% code %}
--- org/openrewrite/java/testing/junit5/MockitoTests.java
+++ org/openrewrite/java/testing/junit5/MockitoTests.java
@@ -3,2 +3,2 @@
package org.openrewrite.java.testing.junit5;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.mockito.Mock;
@@ -16,1 +16,1 @@
List<String> mockedList;
- @Before
+ @BeforeEach
public void initMocks() {
@@ -22,1 +22,1 @@
@Test
- public void usingAnnotationBasedMock() {
+ void usingAnnotationBasedMock() {
{% endcode %} {% endtab %} {% endtabs %}
{% tabs %} {% tab title="org/openrewrite/java/testing/junit5/MockitoTests.java" %}
{% code title="org/openrewrite/java/testing/junit5/MockitoTests.java" %}
package org.openrewrite.java.testing.junit5;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.List;
import static org.mockito.Mockito.verify;
public class MockitoTests {
@Mock
List<String> mockedList;
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void usingAnnotationBasedMock() {
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
{% endcode %}
{% code title="org/openrewrite/java/testing/junit5/MockitoTests.java" %}
package org.openrewrite.java.testing.junit5;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.List;
import static org.mockito.Mockito.verify;
public class MockitoTests {
@Mock
List<String> mockedList;
@BeforeEach
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
void usingAnnotationBasedMock() {
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
{% endcode %}
{% endtab %} {% tab title="Diff" %} {% code %}
--- org/openrewrite/java/testing/junit5/MockitoTests.java
+++ org/openrewrite/java/testing/junit5/MockitoTests.java
@@ -3,2 +3,2 @@
package org.openrewrite.java.testing.junit5;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.mockito.Mock;
@@ -16,1 +16,1 @@
List<String> mockedList;
- @Before
+ @BeforeEach
public void initMocks() {
@@ -22,1 +22,1 @@
@Test
- public void usingAnnotationBasedMock() {
+ void usingAnnotationBasedMock() {
{% endcode %} {% endtab %} {% endtabs %}
This recipe has no required configuration options. It can be activated by adding a dependency on org.openrewrite.recipe:rewrite-testing-frameworks:2.0.2
in your build file or by running a shell command (in which case no build changes are needed):
{% tabs %}
{% tab title="Gradle" %}
{% code title="build.gradle" %}
plugins {
id("org.openrewrite.rewrite") version("6.1.4")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.junit5.JUnit4to5Migration")
}
repositories {
mavenCentral()
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:2.0.2")
}
{% endcode %} {% endtab %} {% tab title="Maven POM" %} {% code title="pom.xml" %}
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.2.4</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.testing.junit5.JUnit4to5Migration</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-testing-frameworks</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
{% endcode %} {% endtab %}
{% tab title="Maven Command Line" %} {% code title="shell" %} You will need to have Maven installed on your machine before you can run the following command.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-testing-frameworks:RELEASE \
-Drewrite.activeRecipes=org.openrewrite.java.testing.junit5.JUnit4to5Migration
{% endcode %} {% endtab %} {% endtabs %}
{% tabs %} {% tab title="Recipe List" %}
- Use wiremock extension
- Use JUnit Jupiter
@Disabled
- Use JUnit Jupiter
Executable
- Remove JUnit 4
@RunWith
annotations that do not require an@ExtendsWith
replacement- obsoleteRunners:
[org.junit.runners.JUnit4, org.junit.runners.BlockJUnit4ClassRunner]
- obsoleteRunners:
- Remove Maven plugin dependency
- pluginGroupId:
org.apache.maven.plugins
- pluginArtifactId:
maven-surefire-plugin
- groupId:
org.apache.maven.surefire
- artifactId:
surefire-junit*
- pluginGroupId:
- Use
MatcherAssert#assertThat(..)
- Use
Assertions#assume*(..)
and Hamcrest'sMatcherAssume#assume*(..)
- Use Mockito JUnit Jupiter extension
- Migrate from JUnit 4
@FixedMethodOrder
to JUnit 5@TestMethodOrder
- Migrate JUnit 4
TestCase
to JUnit Jupiter - JUnit 4
Assert
To JUnit JupiterAssertions
- JUnit 4
@Category
to JUnit Jupiter@Tag
- Cleanup JUnit imports
- Use JUnit Jupiter
@TempDir
- Make
@TempDir
fields non final - JUnit TestName @Rule to JUnit Jupiter TestInfo
- Migrate JUnit 4 lifecycle annotations to JUnit Jupiter
- Make lifecycle methods non private
- Migrate JUnit 4
@Test
annotations to JUnit 5 - JUnit 4
@RunWith(Parameterized.class)
to JUnit Jupiter parameterized tests - Pragmatists @RunWith(JUnitParamsRunner.class) to JUnit Jupiter Parameterized Tests
- JUnit 4
ExpectedException
To JUnit Jupiter'sassertThrows()
- OkHttp 3.x
MockWebServer
@Rule
To 4.xMockWebServer
- Use Vert.x JUnit 5 Extension
- JUnit 4
@RunWith(Enclosed.class)
to JUnit Jupiter@Nested
- JUnit 5 inner test classes should be annotated with
@Nested
- Add
org.hamcrest:hamcrest
if it is used. - Use OkHttp 3 MockWebServer for JUnit 5
- Remove a Gradle or Maven dependency
- groupId:
junit
- artifactId:
junit
- groupId:
- Exclude Maven dependency
- groupId:
junit
- artifactId:
junit
- groupId:
- Remove a Gradle or Maven dependency
- groupId:
org.junit.vintage
- artifactId:
junit-vintage-engine
- groupId:
- Exclude Maven dependency
- groupId:
org.junit.vintage
- artifactId:
junit-vintage-engine
- groupId:
- Add Gradle or Maven dependency
- groupId:
org.junit.jupiter
- artifactId:
junit-jupiter
- version:
5.x
- onlyIfUsing:
org.junit.jupiter.api.Test
- acceptTransitive:
true
- groupId:
- Add Gradle or Maven dependency
- groupId:
org.junit.jupiter
- artifactId:
junit-jupiter-params
- version:
5.x
- onlyIfUsing:
org.junit.jupiter.params.ParameterizedTest
- acceptTransitive:
true
- groupId:
- Upgrade Maven plugin version
- groupId:
org.apache.maven.plugins
- artifactId:
maven-surefire-plugin
- newVersion:
2.22.x
- groupId:
- Upgrade Maven plugin version
- groupId:
org.apache.maven.plugins
- artifactId:
maven-failsafe-plugin
- newVersion:
2.22.x
- groupId:
{% endtab %}
{% tab title="Yaml Recipe List" %}
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.junit5.JUnit4to5Migration
displayName: JUnit Jupiter migration from JUnit 4.x
description: Migrates JUnit 4.x tests to JUnit Jupiter.
tags:
- junit
- testing
recipeList:
- org.openrewrite.java.testing.junit5.UseWiremockExtension
- org.openrewrite.java.testing.junit5.IgnoreToDisabled
- org.openrewrite.java.testing.junit5.ThrowingRunnableToExecutable
- org.openrewrite.java.testing.junit5.RemoveObsoleteRunners:
obsoleteRunners: [org.junit.runners.JUnit4, org.junit.runners.BlockJUnit4ClassRunner]
- org.openrewrite.maven.RemovePluginDependency:
pluginGroupId: org.apache.maven.plugins
pluginArtifactId: maven-surefire-plugin
groupId: org.apache.maven.surefire
artifactId: surefire-junit*
- org.openrewrite.java.testing.junit5.UseHamcrestAssertThat
- org.openrewrite.java.testing.junit5.MigrateAssumptions
- org.openrewrite.java.testing.junit5.UseMockitoExtension
- org.openrewrite.java.testing.junit5.UseTestMethodOrder
- org.openrewrite.java.testing.junit5.MigrateJUnitTestCase
- org.openrewrite.java.testing.junit5.AssertToAssertions
- org.openrewrite.java.testing.junit5.CategoryToTag
- org.openrewrite.java.testing.junit5.CleanupJUnitImports
- org.openrewrite.java.testing.junit5.TemporaryFolderToTempDir
- org.openrewrite.java.testing.junit5.TempDirNonFinal
- org.openrewrite.java.testing.junit5.TestRuleToTestInfo
- org.openrewrite.java.testing.junit5.UpdateBeforeAfterAnnotations
- org.openrewrite.java.testing.junit5.LifecycleNonPrivate
- org.openrewrite.java.testing.junit5.UpdateTestAnnotation
- org.openrewrite.java.testing.junit5.ParameterizedRunnerToParameterized
- org.openrewrite.java.testing.junit5.JUnitParamsRunnerToParameterized
- org.openrewrite.java.testing.junit5.ExpectedExceptionToAssertThrows
- org.openrewrite.java.testing.junit5.UpdateMockWebServer
- org.openrewrite.java.testing.junit5.VertxUnitToVertxJunit5
- org.openrewrite.java.testing.junit5.EnclosedToNested
- org.openrewrite.java.testing.junit5.AddMissingNested
- org.openrewrite.java.testing.hamcrest.AddHamcrestIfUsed
- org.openrewrite.java.testing.junit5.UpgradeOkHttpMockWebServer
- org.openrewrite.java.dependencies.RemoveDependency:
groupId: junit
artifactId: junit
- org.openrewrite.maven.ExcludeDependency:
groupId: junit
artifactId: junit
- org.openrewrite.java.dependencies.RemoveDependency:
groupId: org.junit.vintage
artifactId: junit-vintage-engine
- org.openrewrite.maven.ExcludeDependency:
groupId: org.junit.vintage
artifactId: junit-vintage-engine
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.junit.jupiter
artifactId: junit-jupiter
version: 5.x
onlyIfUsing: org.junit.jupiter.api.Test
acceptTransitive: true
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.junit.jupiter
artifactId: junit-jupiter-params
version: 5.x
onlyIfUsing: org.junit.jupiter.params.ParameterizedTest
acceptTransitive: true
- org.openrewrite.maven.UpgradePluginVersion:
groupId: org.apache.maven.plugins
artifactId: maven-surefire-plugin
newVersion: 2.22.x
- org.openrewrite.maven.UpgradePluginVersion:
groupId: org.apache.maven.plugins
artifactId: maven-failsafe-plugin
newVersion: 2.22.x
{% endtab %} {% endtabs %}
- Patrick Way
- Jonathan Schneider
- Patrick
- Knut Wannheden
- Sam Snyder
- Jonathan Schnéider
- Greg Adams
- Tim te Beek
- Tracey Yoshima
- Greg Adams
- Tim te Beek
- Nick McKinney
- Tyler Van Gorder
- Sofia Britto Schwartz
- Aaron Gershman
- traceyyoshima
- Michael Keppler
- Scott Jungling
- Tim te Beek
- Kun Li
- Kyle Scully
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.