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

[Feature]: Provide a "EnabledIfDockerAvailable" annotation #8613

Closed
eidottermihi opened this issue May 7, 2024 · 1 comment
Closed

[Feature]: Provide a "EnabledIfDockerAvailable" annotation #8613

eidottermihi opened this issue May 7, 2024 · 1 comment

Comments

@eidottermihi
Copy link
Contributor

Module

None

Problem

When using @Testcontainers JUnit Extension, the test gets skipped if no docker is available when using

@Testcontainers(disabledWithoutDocker = true)

However there are cases where @Testcontainers can not be used, e.g. when a Container should live longer than for a single test ("Singleton container"). This is also described in the official docs here: https://java.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers

Using this method, one has to custom build a corresponding @EnabledIf-ish annotation, for example:

@EnabledIf(
        value = "com.example.CustomDockerCheck#isDockerAvailable",
        disabledReason = "docker not available for testcontainers"
)
class MyTest {
 // ...
}
package com.example;

import org.testcontainers.DockerClientFactory;

public class CustomDockerCheck {

    static boolean isDockerAvailable() {
        // from TestcontainersExtension, see org/testcontainers/junit/jupiter/TestcontainersExtension
        try {
            DockerClientFactory.instance().client();
            return true;
        } catch (Throwable ex) {
            return false;
        }
    }

}

Solution

Provide a EnabledIfDockerAvailable annotation that is using the same detection mechanism as @Testcontainers / org.testcontaienrs.junit.jupiter.TestcontainersExtensions

Benefit

It saves people some boilerplate code in their user codebases.

Alternatives

Would you like to help contributing this feature?

Yes

@eddumelendez
Copy link
Member

Hi @eidottermihi, thanks for the suggestion. PR is welcome :)

eidottermihi added a commit to it-at-m/testcontainers-java that referenced this issue May 15, 2024
eidottermihi added a commit to it-at-m/testcontainers-java that referenced this issue May 15, 2024
eidottermihi added a commit to it-at-m/testcontainers-java that referenced this issue May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants