Skip to content

Commit

Permalink
Really reuse Testcontainers
Browse files Browse the repository at this point in the history
The `withReuse(true)` and `testcontainers.reuse.enable=true` don't work together with `@Container`.
The JUnit extension gathers those containers and stop them in the end of test class unconditionally.

* Remove `@Container` annotation usage
* Use `@BeforeAll` and `GenericContainer.start()` manually
This way the container ensures to reuse existing running container and don't start a fresh one.
Since the container instance is stored in a `static` property, it is really started only once.
The rest tests in a suite just reuse that existing container.
Ryuk container will take care about their stopping and removal eventually afte JVM exit.

**Cherry-pick to `5.5.x`**
  • Loading branch information
artembilan authored and garyrussell committed May 19, 2022
1 parent e645d04 commit 7262c5f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

package org.springframework.integration.jdbc.mysql;

import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

/**
* The base contract for JUnit tests based on the container for MqSQL.
* The Testcontainers 'reuse' option must be disabled,so, Ryuk container is started
* and will clean all the containers up from this test suite after JVM exit.
* Since the MqSQL container instance is shared via static property, it is going to be
* started only once per JVM, therefore the target Docker container is reused automatically.
*
* @author Artem Bilan
*
Expand All @@ -30,9 +34,12 @@
@Testcontainers(disabledWithoutDocker = true)
public interface MySqlContainerTest {

@Container
MySQLContainer<?> MY_SQL_CONTAINER = new MySQLContainer<>("mysql").withReuse(true);
MySQLContainer<?> MY_SQL_CONTAINER = new MySQLContainer<>("mysql:8.0.29-oracle");

@BeforeAll
static void startContainer() {
MY_SQL_CONTAINER.start();
}

static String getDriverClassName() {
return MY_SQL_CONTAINER.getDriverClassName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@

package org.springframework.integration.mqtt;

import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

/**
* The base contract for JUnit tests based on the container for MQTT Mosquitto broker.
* The Testcontainers 'reuse' option must be disabled,so, Ryuk container is started
* and will clean all the containers up from this test suite after JVM exit.
* Since the Mosquitto container instance is shared via static property, it is going to be
* started only once per JVM, therefore the target Docker container is reused automatically.
*
* @author Artem Bilan
*
Expand All @@ -29,13 +34,15 @@
@Testcontainers(disabledWithoutDocker = true)
public interface MosquittoContainerTest {

@Container
GenericContainer<?> MOSQUITTO_CONTAINER =
new GenericContainer<>("eclipse-mosquitto:2.0.12")
.withCommand("mosquitto -c /mosquitto-no-auth.conf")
.withReuse(true)
.withExposedPorts(1883);

@BeforeAll
static void startContainer() {
MOSQUITTO_CONTAINER.start();
}

static String mqttUrl() {
return "tcp://localhost:" + MOSQUITTO_CONTAINER.getFirstMappedPort();
Expand Down

0 comments on commit 7262c5f

Please sign in to comment.