0

I have a JUnit test suite that comprises of 3 smaller test suites. When these tests are run, 2 docker containers are spun up (an oracle slim db and an elasticsearch). I want to be able to remove these test containers after all of the tests have finished running.

I have tried a cleanup method in the main test suite which has not worked as below:

@Suite
@SelectClasses({
    IntegrationTestSuite1.class,
    IntegrationTestSuite2.class,
    IntegrationTestSuite3.class,
})
@SuiteDisplayName("Full integration test suite")
@SpringBootTest(classes = MainApplication.class)
public class FullIntegrationTestSuite {

  @AfterAll
  public static void cleanup() {
    String script = "cd ../../../resources; docker rm -f $(docker ps -q)";
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("powershell.exe", "-Command", script);
    try {
      Process process = processBuilder.start();
      process.waitFor();
    } catch (IOException | InterruptedException e) {
      e.printStackTrace();
    }
  }
}

The cleanup method is never invoked after all tests have run. The smaller suites are annotated with @SelectPackages("sometestpackage") which contain the test classes for each package.

Is there another/better way I should be doing this or something incorrect with how I am attempting it above?

2
  • 1
    I would strongly recommends to use the TestContainers. This will take in charge the startup and stop of your containers and you will not need to manage this manually. This Baeldung article shows also how to use it.
    – рüффп
    Commented May 21 at 21:13
  • Mate trying to invent TestContainers here. Also, this is very Windows-locked code. Commented May 21 at 22:33

0

Browse other questions tagged or ask your own question.