/rewrite-TestNG-to-JUnit5

An OpenRewrite recipe to migrate your project from TestNG to JUnit 5 (Jupiter)

Primary LanguageJava

TestNG to Junit5 recipe   Compatible with Java 8

GitHub Actions Workflow StatusSonar Coverage

Converts TestNG test annotations and assertions to Junit 5.

Inspired by the Migrate JUnit 4 @Test annotations to JUnit 5 recipe

Roadmap

This repository is under heavy development. Refer to the project kanban board for ongoing updates on the road to feature completeness. Should you notice a blind spot regarding TestNG features that should be included in a project migration, or if you have idea for a particular implementation, your contributions are welcome!

See below tables for a brief summary of the implementation progress.

Annotations

Annotation on
Class
on
Methods
@Test ✔️ ✔️
@Test(description = "%s") 🔨 #23 ✔️
@Test(enabled = false) 🔨 #39 ✔️
@Test(expectedExceptions = Exception.class) 🔨 #20 ✔️
@Test(expectedExceptionsMessageRegExp = "%s") 🔨 #21 ✔️
@Test(groups = "%s") 🔨 #27 ✔️
@Test(timeOut = "%s") 🔨 #25 ✔️
@DataProvider 🔨 #6 🔨 #38
@Ignore 🔨 #15 🔨 #15
@Test(enabled = CONSTANT_EXPRESSION) 🤔 #35 🤔 #35
@Factory 🤔 #8 🤔 #8
@Test(priority, threadPoolSize) et al. #5 #5

Framework features

Feature on
Class
Tests in inner classes ✔️ #30
Assertions 🔨 #3
Lifecycle annotations 🔨 #7
Per class instantiation lifecycle 🔨 #14
SkipException (→ assumption) 🔨 #32
Dependency migration 🔨#29
Interceptor interfaces 🤔 #10 | #11
Listener interfaces 🤔 #12 | #13

Usage

Prerequisites

  • cloned the project to your local machine

  • if you're an SDKMAN!-Andy, simply run sdk env from the project directory
    otherwise ensure:

    • JDK 17+
    • Maven 3.9.x or Gradle 8.x

    are provided on your system.

Build & install this recipe to your local repository

From the project directory, run one of the following:

Maven

mvn install -DskipTests

Gradle

./gradlew publishToMavenLocal
# or ./gradlew pTML
# or mvn install

This will publish to your local maven repository, typically under ~/.m2/repository.

Migrate a project

Maven

In the pom.xml of a different project you wish to run the recipe on, make it a plugin dependency of rewrite-maven-plugin:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.33.0</version>
        <configuration>
          <activeRecipes>
            <recipe>org.philzen.oss.testng.MigrateToJunit5</recipe>
          </activeRecipes>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.philzen.oss</groupId>
            <artifactId>rewrite-testng-to-junit5</artifactId>
            <version>1.0.1-SNAPSHOT</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

Now run the recipe via mvn rewrite:run.

Gradle

Unlike Maven, Gradle must be explicitly configured to resolve dependencies from Maven local. In the root project of a gradle build that you wish to run this recipe on, make it a dependency of the rewrite configuration:

plugins {
    id("java")
    id("org.openrewrite.rewrite") version("latest.release")
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    rewrite("com.yourorg:rewrite-recipe-starter:latest.integration")
}

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

Now run the recipe via gradlew rewriteRun.