JUnit Jupiter migration from JUnit 4.x
org.openrewrite.java.testing.junit5.JUnit4to5Migration
Migrates JUnit 4.x tests to JUnit Jupiter.
Tags
- junit
- testing
Recipe source
GitHub, Issue Tracker, Maven Central
This recipe is composed of more than one recipe. If you want to customize the set of recipes this is composed of, you can find and copy the GitHub source for the recipe from the link above.
License
This recipe is available under the Moderne Source Available License.
Definition
- Recipe List
- Yaml 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:
- Upgrade Surefire Plugin
- Add Hamcrest JUnit dependency
- 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 - Change method name
- methodPattern:
org.junit.Assert assertEquals(.., Object[], Object[])
- newMethodName:
assertArrayEquals
- methodPattern:
- 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
- Migrate JUnit 4
@Test
annotations to JUnit 5 - Add missing
@BeforeEach
,@AfterEach
,@Test
to overriding methods - 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 XMLUnit Legacy for JUnit 5
- Remove a Gradle or Maven dependency
- groupId:
junit
- artifactId:
junit
- groupId:
- Exclude Maven dependency
- groupId:
junit
- artifactId:
junit
- groupId:
- Remove exclusion
- groupId:
org.testcontainers
- artifactId:
*
- exclusionGroupId:
junit
- exclusionArtifactId:
junit
- groupId:
- Remove exclusion
- groupId:
org.springframework.boot
- artifactId:
spring-boot-testcontainers
- exclusionGroupId:
junit
- exclusionArtifactId:
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 JUnit Jupiter dependencies
- Add Gradle or Maven dependency
- groupId:
org.junit.jupiter
- artifactId:
junit-jupiter-params
- version:
5.x
- onlyIfUsing:
org.junit.jupiter.params.ParameterizedTest
- scope:
test
- acceptTransitive:
true
- groupId:
- Upgrade Maven plugin version
- groupId:
org.apache.maven.plugins
- artifactId:
maven-surefire-plugin
- newVersion:
3.1.x
- groupId:
- Upgrade Maven plugin version
- groupId:
org.apache.maven.plugins
- artifactId:
maven-failsafe-plugin
- newVersion:
3.1.x
- groupId:
- Gradle
Test
use JUnit Jupiter - Change type
- oldFullyQualifiedTypeName:
org.jbehave.core.junit.JUnitStories
- newFullyQualifiedTypeName:
org.jbehave.core.junit.JupiterStories
- oldFullyQualifiedTypeName:
- Migrate rider-spring (JUnit4) to rider-junit5 (JUnit5)
---
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.UpgradeSurefirePlugin
- org.openrewrite.java.testing.junit5.AddHamcrestJUnitDependency
- 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.ChangeMethodName:
methodPattern: org.junit.Assert assertEquals(.., Object[], Object[])
newMethodName: assertArrayEquals
- 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.UpdateTestAnnotation
- org.openrewrite.java.testing.junit5.AddMissingTestBeforeAfterAnnotations
- 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.UseXMLUnitLegacy
- org.openrewrite.java.dependencies.RemoveDependency:
groupId: junit
artifactId: junit
- org.openrewrite.maven.ExcludeDependency:
groupId: junit
artifactId: junit
- org.openrewrite.maven.RemoveExclusion:
groupId: org.testcontainers
artifactId: *
exclusionGroupId: junit
exclusionArtifactId: junit
- org.openrewrite.maven.RemoveExclusion:
groupId: org.springframework.boot
artifactId: spring-boot-testcontainers
exclusionGroupId: junit
exclusionArtifactId: 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.testing.junit5.AddJupiterDependencies
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.junit.jupiter
artifactId: junit-jupiter-params
version: 5.x
onlyIfUsing: org.junit.jupiter.params.ParameterizedTest
scope: test
acceptTransitive: true
- org.openrewrite.maven.UpgradePluginVersion:
groupId: org.apache.maven.plugins
artifactId: maven-surefire-plugin
newVersion: 3.1.x
- org.openrewrite.maven.UpgradePluginVersion:
groupId: org.apache.maven.plugins
artifactId: maven-failsafe-plugin
newVersion: 3.1.x
- org.openrewrite.java.testing.junit5.GradleUseJunitJupiter
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.jbehave.core.junit.JUnitStories
newFullyQualifiedTypeName: org.jbehave.core.junit.JupiterStories
- org.openrewrite.java.testing.dbrider.MigrateDbRiderSpringToDbRiderJUnit5
Usage
This recipe has no required configuration options. It can be activated by adding a dependency on org.openrewrite.recipe:rewrite-testing-frameworks
in your build file or by running a shell command (in which case no build changes are needed):
- Gradle
- Gradle init script
- Maven POM
- Maven Command Line
- Moderne CLI
- Add the following to your
build.gradle
file:
plugins {
id("org.openrewrite.rewrite") version("6.29.0")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.junit5.JUnit4to5Migration")
setExportDatatables(true)
}
repositories {
mavenCentral()
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:2.24.0")
}
- Run
gradle rewriteRun
to run the recipe.
- Create a file named
init.gradle
in the root of your project.
initscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies { classpath("org.openrewrite:plugin:6.29.0") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:2.24.0")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.junit5.JUnit4to5Migration")
setExportDatatables(true)
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
}
- Run the recipe.
gradle --init-script init.gradle rewriteRun
- Add the following to your
pom.xml
file:
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.47.0</version>
<configuration>
<exportDatatables>true</exportDatatables>
<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.24.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
- Run
mvn rewrite:run
to run the recipe.
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 -Drewrite.exportDatatables=true
You will need to have configured the Moderne CLI on your machine before you can run the following command.
mod run . --recipe JUnit4to5Migration
If the recipe is not available locally, then you can install it using:
mod config recipes jar install org.openrewrite.recipe:rewrite-testing-frameworks:2.24.0
See how this recipe works across multiple open-source repositories
Run this recipe on OSS repos at scale with the Moderne SaaS.
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.
Data Tables
Source files that had results
org.openrewrite.table.SourcesFileResults
Source files that were modified by the recipe run.
Column Name | Description |
---|---|
Source path before the run | The source path of the file before the run. null when a source file was created during the run. |
Source path after the run | A recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run. |
Parent of the recipe that made changes | In a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all. |
Recipe that made changes | The specific recipe that made a change. |
Estimated time saving | An estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds. |
Cycle | The recipe cycle in which the change was made. |
Source files that errored on a recipe
org.openrewrite.table.SourcesFileErrors
The details of all errors produced by a recipe run.
Column Name | Description |
---|---|
Source path | The file that failed to parse. |
Recipe that made changes | The specific recipe that made a change. |
Stack trace | The stack trace of the failure. |
Recipe performance
org.openrewrite.table.RecipeRunStats
Statistics used in analyzing the performance of recipes.
Column Name | Description |
---|---|
The recipe | The recipe whose stats are being measured both individually and cumulatively. |
Source file count | The number of source files the recipe ran over. |
Source file changed count | The number of source files which were changed in the recipe run. Includes files created, deleted, and edited. |
Cumulative scanning time | The total time spent across the scanning phase of this recipe. |
99th percentile scanning time | 99 out of 100 scans completed in this amount of time. |
Max scanning time | The max time scanning any one source file. |
Cumulative edit time | The total time spent across the editing phase of this recipe. |
99th percentile edit time | 99 out of 100 edits completed in this amount of time. |
Max edit time | The max time editing any one source file. |