I've introduced testcontainers into our build to have a 1:1 reproduceable build on CI and locally. But some people do not like the idea of docker magic happening in the background when they build locally (weird! I know. Don't ask 馃槃)
I've read about @Testcontainers(disabledWithoutDocker = true) but that's not what is needed in my case, since it would skip the tests as well. But I don't want to skip the tests. I just want to prevent testcontainers to kick in when building locally where the test environtment was already started. (besides that I was not able to use this annotation w/ Junit 5 and Docker Compose)
Is there a way to deactivate testcontainers via command-line similar to mvn verify -DskipTests ? i.e. -DskipTestcontainers
Hi,
I don't know if there is something in the container test library and maybe you already know it, but you can configure this type of stuff on maven build section, in particular you can use the maven-failsafe-plugin. You can rename tests containers in something like UserRepositoryContainerTest.java and use this configuration in your pom.xml
<profile>
<id>containerTest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>%regex[.*ContainerTest.*]</include>
</includes>
<excludes>
<exclude>%regex[.*(Unit|Integration|Contract).*Test.*]</exclude>
</excludes>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
and run only test container using profiles: mvn test -PcontainerTest. in the same way if you run mvn test, test containers will be deactiveted.
Hey @kmos, thanks for the input. Do I understand correctly that you suggest to rename the test class(es)?
In my case it's only this one class: https://github.com/ehrbase/fhir-bridge/blob/develop/src/test/java/org/ehrbase/fhirbridge/FhirBridgeApplicationTestIT.java#L49 So if I exlude it I will have no tests. The issue is that it's the same test that I want be able to run with OR without testcontainers. It's not about particular tests that depent on some containers, it's about having more control over whether to execute test with or without testcontainers(.org) in general.
Yes, my suggestion was to split tests by type using a convention on names like UserRepositoryUnitTest, UserRepositoryIntegrationTest, UserRepositoryContainerTest and execute the right category with maven profiles. Obviously, I forgot to mention that in the default configuration of maven-failsafe-plugin you should configure the tests that you want to execute always (like unit test).
Anyway, if I understand correctly you don't want to skip tests but run the same test without spinning a container right?. Taking a briefly look at your code, may be you can active/deactive test container using a Spring Configuration bean in which you set up your container like in this example and use a spring profile in order to deactive it.
I hope that it can be helpful.
if I understand correctly you don't want to skip tests but run the same test without spinning a container right?
yep exactly 馃槂
I guess if there is not something that is as easy as mvn verify -DskiptTestcontainers I'll have to ask our team if they feel it's worth the effort to implement something to make testcontainers optional. On the CI pipeline I actually don't really need testcontainers.
Hi @wlad !
I have absolutely the same case. I solved this problem. Oracle case:
1) Use test-containtrs-springboot https://github.com/testcontainers/testcontainers-spring-boot for running needed container
2) Configure application.yml for CI case.
application-testcontainers.yml
spring:
datasource:
url: jdbc:oracle:thin:#your-ci-jdbc-url
username: ci-user # inject it from command line or env
password: ci-pass # inject it from command line or env
And bootstrap.yml
embedded:
containers:
enabled: false
This is the configuration for CI and it works without any specific flags.
3) For local running configure testcontainers profile
application-testcontainers.yml
spring:
datasource:
url: jdbc:oracle:thin:@${embedded.oracle.host}:${embedded.oracle.port}/${embedded.oracle.database}
username: ${embedded.oracle.user}
password: ${embedded.oracle.password}
And bootstrap-testcontainers.yml
embedded:
oracle:
enabled: true
containers:
enabled: true
And run it with -Dspring.profiles.active=testcontainers flag.
Hope you use spring-boot and testcontainers-spring-boot support your container type :)
Anyway, you can apply profiles-based approach to any spring project.
Most helpful comment
Hi @wlad !
I have absolutely the same case. I solved this problem. Oracle case:
1) Use test-containtrs-springboot https://github.com/testcontainers/testcontainers-spring-boot for running needed container
2) Configure application.yml for CI case.
application-testcontainers.ymlAnd
bootstrap.ymlThis is the configuration for CI and it works without any specific flags.
3) For local running configure
testcontainersprofileapplication-testcontainers.ymlAnd
bootstrap-testcontainers.ymlAnd run it with
-Dspring.profiles.active=testcontainersflag.Hope you use spring-boot and testcontainers-spring-boot support your container type :)
Anyway, you can apply profiles-based approach to any spring project.