MBoegers / migrate-testngtojupiter-rewrite

Open Rewrite Recipes to migrate your Test NG unit tests to JUnit Jupiter
Eclipse Public License 2.0
5 stars 0 forks source link

Add Recipe to migrate DataProvider annotation #1

Closed MBoegers closed 6 months ago

MBoegers commented 8 months ago

The data-driven tests are useful whenever the same test should be executed with different data. In testing this is a common task and therefore has to be migrated to JUnit Jupiter. As JUnit Team writes here the Jupiter equivalent is @ParameterizedTest in combination with a @MethodSource annotation.

DataProvider documentation from TestNG

Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.

https://testng.org/#_annotations

Example

In OpenJDK there are exmaples of data-driven with TestNG, one is:

JUnit Jupiter Parameterized tests

In JUnit Jupiter data-driven are done with Parameterized tests with parameter sources. A similar approach as in TestNG is possible with a Method Source which can also be in external classes.

https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

Example

The TestNG data-driven test from OpenJDK could be migrated from

package jdk.vm.ci.hotspot.test;

import org.testng.annotations.DataProvider;
public class BoxPrimitiveDataProvider {
    @DataProvider(name = "boxPrimitiveDataProvider")
    public static Object[][] boxPrimitiveDataProvider() { /*...*/ }
}

import org.testng.annotations.Test;
public class HotSpotConstantReflectionProviderTest {
    @Test(dataProvider = "unboxPrimitiveDataProvider", dataProviderClass = UnboxPrimitiveDataProvider.class)
    public void testUnboxPrimitive(JavaConstant constant, JavaConstant expected) {/*...*/}
}

into

package jdk.vm.ci.hotspot.test;

import org.junit.jupiter.params.provider.Arguments;
import java.util.Arrays;
public class BoxPrimitiveDataProvider {
    // generic wrapper from Object[][] to Stream<Arguments>
    public static Stream<Arguments> boxPrimitiveDataProviderSource() {
        return Arrays.stream(boxPrimitiveDataProvider()).map(Arguments::of);
    }
    public static Object[][] boxPrimitiveDataProvider() { /*...*/ }
}

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class HotSpotConstantReflectionProviderTest {
    @ParameterizedTest
    @MethodSource("jdk.vm.ci.hotspot.test.BoxPrimitiveDataProvider#boxPrimitiveDataProviderSource")
    public void testUnboxPrimitive(JavaConstant constant, JavaConstant expected) {/*...*/}
}
MBoegers commented 8 months ago

Remark

It is importend to notice that the Test annotation is replaced by ParameterizedTest in this recipe.

MBoegers commented 6 months ago

its done in #10