Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recipe to migrate SkipException to Assumptions #4

Open
MBoegers opened this issue Mar 2, 2024 · 0 comments
Open

Add recipe to migrate SkipException to Assumptions #4

MBoegers opened this issue Mar 2, 2024 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@MBoegers
Copy link
Owner

MBoegers commented Mar 2, 2024

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!

@MBoegers MBoegers added enhancement New feature or request help wanted Extra attention is needed labels Mar 2, 2024
@MBoegers MBoegers added this to the V1.0 milestone Mar 2, 2024
@MBoegers MBoegers removed the help wanted Extra attention is needed label Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant