File tree 4 files changed +76
-0
lines changed
main/groovy/org/testcontainers/spock
test/groovy/org/testcontainers/spock
4 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ package org.testcontainers.spock
2
+
3
+ import org.testcontainers.DockerClientFactory
4
+
5
+ class DockerAvailableDetector {
6
+
7
+ boolean isDockerAvailable () {
8
+ try {
9
+ DockerClientFactory . instance(). client();
10
+ return true ;
11
+ } catch (Throwable ex) {
12
+ return false ;
13
+ }
14
+ }
15
+ }
Original file line number Diff line number Diff line change @@ -54,4 +54,11 @@ import java.lang.annotation.Target
54
54
@Target ([ElementType . TYPE , ElementType . METHOD ])
55
55
@ExtensionAnnotation (TestcontainersExtension )
56
56
@interface Testcontainers {
57
+
58
+ /**
59
+ * Whether tests should be disabled (rather than failing) when Docker is not available. Defaults to
60
+ * {@code false }.
61
+ * @return if the tests should be disabled when Docker is not available
62
+ */
63
+ boolean disabledWithoutDocker () default false ;
57
64
}
Original file line number Diff line number Diff line change @@ -7,8 +7,23 @@ import org.spockframework.runtime.model.SpecInfo
7
7
8
8
class TestcontainersExtension extends AbstractAnnotationDrivenExtension<Testcontainers > {
9
9
10
+ private final DockerAvailableDetector dockerDetector
11
+
12
+ TestcontainersExtension () {
13
+ this (new DockerAvailableDetector ())
14
+ }
15
+
16
+ TestcontainersExtension (DockerAvailableDetector dockerDetector ) {
17
+ this . dockerDetector = dockerDetector
18
+ }
19
+
10
20
@Override
11
21
void visitSpecAnnotation (Testcontainers annotation , SpecInfo spec ) {
22
+ if (annotation. disabledWithoutDocker()) {
23
+ if (! dockerDetector. isDockerAvailable()) {
24
+ spec. skip(" disabledWithoutDocker is true and Docker is not available" )
25
+ }
26
+ }
12
27
def listener = new ErrorListener ()
13
28
def interceptor = new TestcontainersMethodInterceptor (spec, listener)
14
29
spec. addSetupSpecInterceptor(interceptor)
Original file line number Diff line number Diff line change
1
+ package org.testcontainers.spock
2
+
3
+ import org.spockframework.runtime.model.SpecInfo
4
+ import spock.lang.Specification
5
+ import spock.lang.Unroll
6
+
7
+ class TestcontainersExtensionTest extends Specification {
8
+
9
+ @Unroll
10
+ def " should handle disabledWithoutDocker=#disabledWithoutDocker and dockerAvailable=#dockerAvailable correctly" () {
11
+ given :
12
+ def dockerDetector = Mock (DockerAvailableDetector )
13
+ dockerDetector. isDockerAvailable() >> dockerAvailable
14
+ def extension = new TestcontainersExtension (dockerDetector)
15
+ def specInfo = Mock (SpecInfo )
16
+ def annotation = disabledWithoutDocker ?
17
+ TestDisabledWithoutDocker . getAnnotation(Testcontainers ) :
18
+ TestEnabledWithoutDocker . getAnnotation(Testcontainers )
19
+
20
+ when :
21
+ extension. visitSpecAnnotation(annotation, specInfo)
22
+
23
+ then :
24
+ skipCalls * specInfo. skip(" disabledWithoutDocker is true and Docker is not available" )
25
+
26
+ where :
27
+ disabledWithoutDocker | dockerAvailable | skipCalls
28
+ true | true | 0
29
+ true | false | 1
30
+ false | true | 0
31
+ false | false | 0
32
+ }
33
+
34
+ @Testcontainers (disabledWithoutDocker = true )
35
+ static class TestDisabledWithoutDocker {}
36
+
37
+ @Testcontainers
38
+ static class TestEnabledWithoutDocker {}
39
+ }
You can’t perform that action at this time.
0 commit comments