testcontainers | Selenide + TestContainers sample project | Continuous Deployment library
kandi X-RAY | testcontainers Summary
kandi X-RAY | testcontainers Summary
This is a sample project showing how to run tests in Docker with help of TestContainer library.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of testcontainers
testcontainers Key Features
testcontainers Examples and Code Snippets
Community Discussions
Trending Discussions on testcontainers
QUESTION
I currently have a few integration tests that are running fine, however I'm using the annotation @QuarkusTestResource to launch containers via testcontainers before quarkus is launched and I adapt my properties to the random test container port by overriding the start method of my container class extending QuarkusTestResourceLifecycleManager.
To optimize a little bit I'd like for example to reuse my container that launches kafka since it takes 6-8 seconds to start it and reuse it for several of my integration tests. I've not been able to do so. Since Quarkus manages the lifecycle every times it stops, between every test class, it also stops every container. I tried mixing singleton containers from testcontainers with the Quarkus test resources but it doesn't work. Here's a snippet of the start of one of my integration test class :
...ANSWER
Answered 2022-Apr-01 at 15:30You can use the experimental reusable mode for your container via .withReuse(true)
.
Please note that in order for this to work you need to:
- set the
testcontainers.reuse.enable=true
property in the~/.testcontainers.properties
file - NOT call
.close()
(which programmatically stops the container in any way)
Once you've setup everything accordingly your container should
- not be stopped/garbage collected at the end of the test run anymore
- be picked up again at the next test run - avoiding a new container start
Please note that all runtime modifications (configuration, data, topics etc.) will still be there at the next test run. You need to take suitable measures to avoid non-deterministic tests - like using different / random topic names for each test run.
Also note that you'll need to manually stop/delete the container when you're done with it. Testcontainers will not remove the container for you anymore.
You can also read this article that describes how to do it from spring for inspiration: Reuse Containers With Testcontainers for Fast Integration Test
QUESTION
Is it possible to use Testcontainers with Podman in Java tests? As of March 2022 Testcontainers library doesn't detect an installed Podman as a valid Docker environment.
Can Podman be a Docker replacement on both MacOS with Apple silicon (local development environment) and Linux x86_64 (CI/CD environment)?
...ANSWER
Answered 2022-Mar-28 at 17:00It is possible to use Podman with Testcontainers in Java projects, that use Gradle on Linux and MacOS (both x86_64 and Apple silicon).
Prerequisites- Podman Machine and Remote Client are installed on MacOS - https://podman.io/getting-started/installation#macos
- Podman is installed on Linux - https://podman.io/getting-started/installation#linux-distributions
Testcontainers library communicates with Podman using socket file.
LinuxStart Podman service for a regular user (rootless) and make it listen to a socket:
QUESTION
I am doing an integration test with testcontainers
and spring-boot
and I am having an issue while initializing the scripts. I have 2 scripts: schema.sql
and data.sql
.
When I use DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD
it works fine, but it is not a good idea to rerun a new container after each test. Of course that make the tests very slow.
At the other hand when I use DirtiesContext.ClassMode.AFTER_CLASS
I have this exception:
org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [sql/mariadb/schema.sql]: DROP TABLE IF EXISTS
client
; nested exception is java.sql.SQLIntegrityConstraintViolationException: (conn=4) Cannot delete or update a parent row: a foreign key constraint fails at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:282) ~[spring-jdbc-5.3.13.jar:5.3.13] at ... Caused by: java.sql.SQLIntegrityConstraintViolationException: (conn=4) Cannot delete or update a parent row: a foreign key constraint fails ... Caused by: org.mariadb.jdbc.internal.util.exceptions.MariaDbSqlException: Cannot delete or update a parent row: a foreign key constraint fails
The base class:
...ANSWER
Answered 2022-Mar-28 at 08:20The @Sql
will be executed, per default, BEFORE_TEST_METHOD
. So this is run before each test method. In other words, before any test is run, that SQL is executed. But of course, by re-using the same database for multiple tests, this can run into an error, if the sql script isn't "idempotent", in other words, if the sql script cannot be applied safely twice to the same database.
To make it work, there are multiple possibilities, for example:
adding one for the
AFTER_TEST_METHOD
to cleanup. This then would basically remove all the stuff that was added by this test (including the things added by the@Sql
before). This way, your db is "cleaned up" after each test run and every run can apply the same sql script again safely.Or make it safe to be executed multiple times. This depends on the scripts, but if you can write the SQL in a way that allows you to run it twice without error, this would also work.
Without using
@Sql
, you could also configure aDatabasePopulator
bean in your test config. This way, your SQL code would only run ONCE, when the whole application context is created.You could also try using
@Transactional
on your test, which would make Spring wrap a transaction around your test execution and roll it back afterwards. Unfortunately I have currently no clue how nice this plays with@Sql
, might work, might not, but I would give it a 75% chance of working. And of course, it has some implications (nothing is actually finally written to the DB and if you are using transactions inside your code, there might be complications).
All of these methods would solve your problem, probably.
QUESTION
I'm using Quarkus (2.7.3.Final) with Postgres (quarkus-jdbc-postgresql
).
And I really like Quarkus' approach that if you configure no username
, password
and url
for your datasource it will try to start a testcontainer and emulate the database, when you start the app in development mode.
So for example if you define this in your application.yml
(or application.properties
), Quarkus will start a Postgres testcontainer for you, when you start the app with ./mvnw clean quarkus:dev
:
ANSWER
Answered 2022-Mar-04 at 23:05Following Quarkus' official documentation,
If a profile does not define a value for a specific attribute, the default (no profile) value is used
This behaviour will be useful in many cases, but in yours might lead to the inability to override properties once defined in the default profile back to their empty state.
I would suggest you to swap your profiles around i.e. treat the null-valued dev
configuration as a default and provide meaningful non-null prod
values in an overriding profile.
If you are worried that dev
values might be used this way accidentally in prod
environment, remember that Quarkus is going to use prod
profile by default if not told otherwise.
QUESTION
I am having trouble getting Selenium TestContainers to record the tests for me.
I am using TestContainers in Quarkus. The Quarkus way to deal with setting up test resources in a QuarkusTestResourceLifecycleManager
class, as such:
ANSWER
Answered 2022-Mar-04 at 09:06Best-case I would love to be able to trigger recording in a standard junit @AfterEach method, but the arguments required (TestDescription description, Optional throwable) aren't available...
This is what I would recommend for more advanced framework integrations that don't work OOTB. You should be able instatiate a TestDescription
for your use case e.g., from an anonymous class (it is used to infer the recording's file name).
QUESTION
Hi I am trying to spin up a test container from my unit test class.
from my test class i am using the following
...ANSWER
Answered 2022-Feb-24 at 16:03By accident stumbled accross here: I had the same issue when I was not running the docker engine and the service. Have you docker installed and is the service running?
QUESTION
I execute some tests in GitHub Actions using Testcontainers.
Testcontainers pulls the images which are used in my tests. Unfortunately the images are pulled again at every build.
How can I cache the images in GitHub Actions?
...ANSWER
Answered 2022-Feb-19 at 08:28There's no official support from GitHub Actions (yet) to support caching pulled Docker images (see this and this issue).
What you can do is to pull the Docker images, save them as a .tar
archive and store them in a folder for the GitHub Actions cache action to pick it up.
A sample workflow can look like the following:
QUESTION
I'm having trouble getting liquibase to execute my sql script in my SpringBoot Kotlin appllication.
Here is my build.gradle.kts
...ANSWER
Answered 2022-Feb-19 at 00:59While running your setup with debug logs enabled, I've noticed that auto-configuration for Liquibase did not work, as some criteria was not met.
QUESTION
I'm using both r2dbc and Liquibase in the same application. However, Liquibase is not able to run migrations using r2dbc so I need to use a separate jdbc driver just for it.
I followed the solution here, and used testcontainers for testing, so my application-test.yaml
looks exactly like this:
ANSWER
Answered 2022-Feb-14 at 15:05When you use Testcontainers' JDBC support, which you configure by adding tc
in the jdbc url, the lifecycle of the container is managed automatically.
Since you have two different urls instrumented like that, you get 2 containers.
Instead you can choose a different way to manage the lifecycle that gives you more control.
You can either do it yourself by creating a containers instances and calling start()/stop()
or for example use JUnit integration which will correspond containers lifecycle with the tests lifecycle.
For example for JUnit5, you mark you class with @Testcontainers
and the fields with @Container
, something like:
QUESTION
I'm including an integration test into my application using library "https://www.testcontainers.org/".
This library need a docker environment to run "gradlew test" command.
How would you use a docker image (which include docker and Gradle) in a gitlab-ci.yml
?
ANSWER
Answered 2022-Feb-14 at 09:22That looks like what Christos Sotiriou described in "Your Private CI/CD using self-hosted GitLab and Docker" in 2020.
It involves a GitLab runner:
A GitLab runner has many modes it can operate, which represent how the build will be performed. Among other modes, there is support for spawning Kubernetes pods, or Docker containers to perform builds.
For the sake of simplicity, we will use the plain docker mode, which will spawn a new container with an image of your choice (determined by your Dockerfile)
Run the following to your terminal:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install testcontainers
You can use testcontainers like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the testcontainers component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page