I'd like to use a container shared among tests as early in a BeforeAll method like this:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
public class SomeTest {
@Container
private static final GenericContainer aContainer = new GenericContainer("postgres:9");
@BeforeAll
static void doSomethingWithAContainer() {
Assertions.assertTrue(aContainer.isRunning());
}
@Test
void aTest() {
Assertions.assertTrue(aContainer.isRunning());
}
}
I deliberately used a generic container here, but the result is the same with dedicated once, too.
The use case is providing fixtures etc. I wonder if implementing the BeforeAllCallback as it was done beforehand in #887 would have been the better approach, as it should have kept the order (first extensions, than concrete BeforeAll).
@britter I think static containers should indeed be initialized by implementing BeforeAllCallback.
I've added the corresponding failing test case and added a quite naive fix based on BeforeAllCallback interface.
I've probably missed some edge cases, but all current tests are green, so I'll just push it to a PR an we can have a look there.
I tried to use this again and it still fails for me. I have used your test for maven based reproducer, using latest JUnit 5. See
tcreproducer.zip Either we oversaw something in your CI or I am doing something stupid and I just can't spot it.
Most helpful comment
I've added the corresponding failing test case and added a quite naive fix based on
BeforeAllCallbackinterface.I've probably missed some edge cases, but all current tests are green, so I'll just push it to a PR an we can have a look there.