[Feature]: Provide a "EnabledIfDockerAvailable" annotation #8613
Closed
Description
opened on May 7, 2024
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