Skip to content

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

Closed
@eidottermihi

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions