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

junit5 compatible version #11

Closed
davidkarlsen opened this issue Aug 7, 2018 · 9 comments
Closed

junit5 compatible version #11

davidkarlsen opened this issue Aug 7, 2018 · 9 comments

Comments

@davidkarlsen
Copy link

Would it be possible to alter the rule so that @EnableRuleMigrationSupport would work: https://medium.com/@GalletVictor/migration-from-junit-4-to-junit-5-d8fe38644abe

  • or better - make it an Extension?
@davidkarlsen
Copy link
Author

Linking stefanbirkner/system-rules#55

@stefanbirkner
Copy link
Owner

stefanbirkner commented Aug 8, 2018

I would like to avoid implementing ExternalResource because the migration support is only a hack that relies on a contract that is not really given (although it works for almost all rules that implement ExternalResource).

According to your comment in stefanbirkner/system-rules#55 the only problem why you cannot replace the rule with stefanbirkner/fake-sftp-server-lambda is that you need the port information before the Spring extension starts the application. Am I right?

@davidkarlsen
Copy link
Author

Correct

@stefanbirkner
Copy link
Owner

stefanbirkner commented Aug 8, 2018

How did you set the port for the SpringRunner with JUnit 4?

@davidkarlsen
Copy link
Author

private static final String DHUB_ENDPOINT_PROPERTY = "dhub.sftp.endpoint";

    @ClassRule
    public static final FakeSftpServerRule sftpServer = new FakeSftpServerRule();

    @AfterAll
    public static void afterClass() {
        System.clearProperty(DHUB_ENDPOINT_PROPERTY);
    }

    @BeforeAll
    public static void beforeClass() {
        System.setProperty(DHUB_ENDPOINT_PROPERTY, "localhost:" + sftpServer.getPort());
    }

@stefanbirkner
Copy link
Owner

I have a workaround for the lambda solution

private static final int String SFTP_SERVER_PORT = freePort();

@AfterAll
public static void afterClass() {
    System.clearProperty(DHUB_ENDPOINT_PROPERTY);
}

@BeforeAll
public static void beforeClass() {
    System.setProperty(DHUB_ENDPOINT_PROPERTY, "localhost:" + SFTP_SERVER_PORT);
}

@Test
void aTest() {
    withSftpServer(server -> {
        //your code
    });
}

private withSftpServer(
    FakeSftpServer.ExceptionThrowingConsumer testCode
) throws Exception {
    FakeSftpServer.withSftpServer(server -> {
        server.setPort(SFTP_SERVER_PORT);
        testCode.accept(server);
    });
}

private static freePort() {
    try (ServerSocket socket = new ServerSocket(0)) {
        return socket.getLocalPort();
    } catch (IOException e) {
        throw new RuntimeException(
            "Failed to find a free port.",
            e
        );
    }
}

Instead of the method freePort you may also use Spring's SocketUtils.findAvailableTcpPort().

@sbrannen
Copy link

Glad you guys found a work-around! 🎉

@sbrannen
Copy link

And... I can also highly recommend Spring's SocketUtils (I promise: no personal bias).

@davidkarlsen
Copy link
Author

It worked fine. Closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants