Testcontainers-java: JUnit 5 extension starts container to late.

Created on 18 Dec 2018  路  3Comments  路  Source: testcontainers/testcontainers-java

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).

resolutioawaiting-release typbug

Most helpful comment

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.

All 3 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings