'Test containers: manipulate existing docker compose services

I'm using Maven pre-integration-tests and post-integration tests to setup and tear down a test environment with three services (say A, B and C)

Is it possible using Test containers to restart one of those services before a test? If not, can I do that using other docker library for Java?

Thanks!



Solution 1:[1]

If your test is something similar to this.

@Container
public GenericContainer container = new GenericContainer(...);

You can use junit5 BeforeEach and AfterEach annotations to restart your containers before/after running your tests cases.

@BeforeEach
public void beforeEach() {
  container.start();
}

@AfterEach
public void afterEach() {
  container.stop();
}

If you need to restart the container only before specific test, you can try reorganizing your test methods using @Nested annotation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 usuario