MBoegers/migrate-testngtojupiter-rewrite

Add recipe to migrate SkipException to Assumptions

Opened this issue · 0 comments

With throwing a SkipException in a TestNG Test it is indicated that this test should be skipped because of not met prerequisites.
In JUnit Jupiter the same is achievable with the Assumptions API.

TestNG Example

This example is from OpenJDK https://github.com/openjdk/jdk/blob/master/test/jdk/com/sun/net/httpserver/simpleserver/CommandLineNegativeTest.java#L206

import jdk.test.lib.Platform;
import org.testng.SkipException;
import org.testng.annotations.Test;
public class CommandLineNegativeTest {
    @Test(dataProvider = "directoryOptions")
    public void testRootNotReadable(String opt) throws Throwable {
        if (Platform.isWindows()) {
            // Not applicable to Windows. Reason: cannot revoke an owner's read
            // access to a directory that was created by that owner
            throw new SkipException("cannot run on Windows");
        }
        //...
    }
}

JUnit Jupiter

This test is relatively easy to migrate with the Assumptions API.

import jdk.test.lib.Platform;
import org.testng.SkipException;
import org.testng.annotations.Test;
public class CommandLineNegativeTest {
    @Test(dataProvider = "directoryOptions")
    public void testRootNotReadable(String opt) throws Throwable {
        // Not applicable to Windows. Reason: cannot revoke an owner's read
        // access to a directory that was created by that owner
        Assumptions.assumeFalse(Platform.isWindows(), "cannot run on Windows");
        //...
    }
}

Remarks

Usually the block where a SkipException is thrown is guarded by a boolean expression in an if.
This exception can be reused but in the assumeFalse.
It is important to flip the semantics here!