italktothewind
italktothewind

Reputation: 2195

Testcontainers start before Spring app and shut down after Spring App

I have this integration test:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringApplication.class, webEnvironment = DEFINED_PORT)
public class UspControllerIT {

    @Test
    public void someIntegrationTest() {
       ...
    }
}

My Spring Application is using a database, a queue and other stuff I put inside docker-compose.yml.

I want to start my Docker compose BEFORE the Spring application and to stop it AFTER the Spring application has been shutted down (I don't want to see errors in the app due to connections that could not be established or were closed).

Is there any way using test containers?

Thanks in advance.

Upvotes: 0

Views: 1354

Answers (1)

Vadim Yemelyanov
Vadim Yemelyanov

Reputation: 442

You can initiate it as a Singleton container using static variable somewhere in your code.

After you application will stop - they will be automatically stopped.

abstract class TestContainers {
     public static DockerComposeContainer environment;

 static {
    environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
        .withExposedService("redis_1", 4321, Wait.forListeningPort())
        .waitingFor("db_1", Wait.forLogMessage("started", 1))
        .withLocalCompose(true);
}
}

And after that you should extend your classes with it

class MyComposedTests extends TestContainers {

@Test
void init() {
}
}

Upvotes: 0

Related Questions