Skip to content

JUnit 5 extension starts container to late. #1019

@michael-simons

Description

@michael-simons

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions