Skip to content

Latest commit

 

History

History
174 lines (142 loc) · 5.13 KB

runnertoextension.md

File metadata and controls

174 lines (142 loc) · 5.13 KB

JUnit 4 @RunWith to JUnit Jupiter @ExtendWith

org.openrewrite.java.testing.junit5.RunnerToExtension

Replace runners with the JUnit Jupiter extension equivalent.

Source

GitHub, Issue Tracker, Maven Central

  • groupId: org.openrewrite.recipe
  • artifactId: rewrite-testing-frameworks
  • version: 2.0.2

Options

Type Name Description
List runners The fully qualified class names of the JUnit 4 runners to replace. Sometimes several runners are replaced by a single JUnit Jupiter extension.
String extension The fully qualified class names of the JUnit Jupiter extension.

Example

Parameters
Parameter Value
runners List.of("org.mockito.runners.MockitoJUnitRunner")
extension org.mockito.junit.jupiter.MockitoExtension

{% tabs %} {% tab title="MyTest.java" %}

Before

{% code title="MyTest.java" %}

import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MyTest {
}

{% endcode %}

After

{% code title="MyTest.java" %}

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class MyTest {
}

{% endcode %}

{% endtab %} {% tab title="Diff" %} {% code %}

--- MyTest.java
+++ MyTest.java
@@ -1,2 +1,2 @@
-import org.junit.runner.RunWith;
-import org.mockito.runners.MockitoJUnitRunner;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;

@@ -4,1 +4,1 @@
import org.mockito.runners.MockitoJUnitRunner;

-@RunWith(MockitoJUnitRunner.class)
+@ExtendWith(MockitoExtension.class)
public class MyTest {

{% endcode %} {% endtab %} {% endtabs %}

Usage

This recipe has required configuration parameters. Recipes with required configuration parameters cannot be activated directly. To activate this recipe you must create a new recipe which fills in the required parameters. In your rewrite.yml create a new recipe with a unique name. For example: com.yourorg.RunnerToExtensionExample. Here's how you can define and customize such a recipe within your rewrite.yml:

{% code title="rewrite.yml" %}

---
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.RunnerToExtensionExample
displayName: JUnit 4 `@RunWith` to JUnit Jupiter `@ExtendWith` example
recipeList:
  - org.openrewrite.java.testing.junit5.RunnerToExtension:
      runners: org.springframework.test.context.junit4.SpringRunner
      extension: org.springframework.test.context.junit.jupiter.SpringExtension

{% endcode %}

Now that com.yourorg.RunnerToExtensionExample has been defined activate it and take a dependency on org.openrewrite.recipe:rewrite-testing-frameworks:2.0.2 in your build file: {% tabs %} {% tab title="Gradle" %} {% code title="build.gradle" %}

plugins {
    id("org.openrewrite.rewrite") version("6.1.4")
}

rewrite {
    activeRecipe("com.yourorg.RunnerToExtensionExample")
}

repositories {
    mavenCentral()
}

dependencies {
    rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:2.0.2")
}

{% endcode %} {% endtab %} {% tab title="Maven" %} {% 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>com.yourorg.RunnerToExtensionExample</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 %} {% endtabs %}

Contributors

See how this recipe works across multiple open-source repositories

Moderne Link Image

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.