Open Rewrite

Kickstarter Recipe Dev

Java Software Engineer

Line of Business Public

Merlin Bögershausen / @mboegie

Idee

TestNG

JUnit 🪐

😂 EASY! 🤣

🧾JDK/JDK-8301767

📝GitHub PR

📋ToDo’s

  1. Migrate @Before to JUnit 🪐 API

  2. Migrate Assertions methods

  3. Migrate @Test Annotation

  4. Grab a drink 🍹🥂🍻🍷☕️

Setup

IntelliJ Idea 2024.1

Bildschirmfoto 2024 04 08 um 13.33.26

Project

  1. ⤵️ Moderne Inc./Rewrite Recipe Starter

  2. 👩🏼‍💻Manipulate GAV

  3. verify setup with mvn test

  4. 🚮 Example Content

Dependency

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.7.0</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <scope>compile</scope>
</dependency>

🧾 Development

✅ Migrate Setup

FromTo

@BeforeClass

@BeforeAll

@AfterClass

@AfterAll

@BeforeMethod

@BeforeEach

@AfterMethod

@AfterEach

🧾 Migrate Setup

src/main/resources/META-INF/rewrite

type: specs.openrewrite.org/v1beta/recipe
name: io.github.mboegers.openrewrite.TestNgToJUnitJupiter
displayName: Migrate Test NG tests to JUnit Jupiter
description: Migrate Test NG annotations and assertions ...
recipeList:
  - org.openrewrite.java.ChangeType:
      oldFullyQualifiedTypeName: org.testng.annotations.BeforeClass
      newFullyQualifiedTypeName: org.junit.jupiter.api.BeforeAll
      ignoreDefinition: true

✅ Migrate Assertions

import org.testng.Assert;

class MyTest {
  void testMethod() {
    boolean actual, expected;
    Assert.assertEquals(actual, expected, "😣");
  }
}
import org.junit.jupiter.api.Assertions;

class MyTest {
  void testMethod() {
    boolean actual, expected;
    Assertions.assertEquals(expected, actual, "😣");
  }
}

🧾Migrate Assertions

@RecipeDescriptor(
  name = "Replace `Assert#assertEquals(?, ?)`",
  description = "Replace `org.testng.Assert#assertEquals(?, ?)` with `org.junit.jupiter.api.Assertions#assertEquals(?, ?)`."
)
public static class MigrateObjectAssert {
  @BeforeTemplate
  void before(Object actual, Object expected) {
    Assert.assertEquals(actual, expected);
  }
  @AfterTemplate
  void after(Object actual, Object expected) {
    Assertions.assertEquals(expected, actual);
  }
}

✅ Migrate @Test

import org.testng.annotations.Test;

class MyTest {
  @Test()
  void test() {}
}
import org.junit.jupiter.api.Test;

class MyTest {
  @Test
  void test() {}
}

Edge case

import org.testng.annotations.Test;

class MyTest {
  @Test(enabled = false)
  void test() {}
}

🧾 Migrate @Test

If a MethodDeclaration

  • annotated with o.t.a.Test

  • and @Test has not Parameter

then

  1. add @o.j.j.a.Test

  2. add import o.j.j.a.Test

  3. remove o.t.a.Test with Remove annotation

Call for Action

Further Information

Renovate Java with Java

Join me on helping OpenJDK to migrate from TestNG to JUnit 🪐

Contact

Me

MBoegie@fosstodon.org // @MBoegi // MBoegers

adesso

Infos // DevBlog

Approach direct

I'm here all day with cookies!

Jobs

qrcode adesso jobs

Image Credits