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?