0

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.

italktothewind
  • 1,950
  • 2
  • 28
  • 55

1 Answers1

0

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() {
}
}
  • That solution will not shut down the containers once the tests finish. – italktothewind Jul 28 '23 at 21:35
  • @italktothewind Actually it will, testcontainers run image testcontainers/ryuk along with other containers you added, and it will kill all of them after your tests are finished. Unless you disabled it of course. – Vadim Yemelyanov Jul 28 '23 at 23:33