Annotation Interface Testcontainers


@Target(TYPE) @Retention(RUNTIME) @ExtendWith(TestcontainersExtension.class) @Inherited public @interface Testcontainers
@Testcontainers is a JUnit Jupiter extension to activate automatic startup and stop of containers used in a test case.

The Testcontainers extension finds all fields that are annotated with Container and calls their container lifecycle methods. Containers declared as static fields will be shared between test methods. They will be started only once before any test method is executed and stopped after the last test method has executed. Containers declared as instance fields will be started and stopped for every test method.

The annotation @Testcontainers can be used on a superclass in the test hierarchy as well. All subclasses will automatically inherit support for the extension.

Note: This extension has only been tested with sequential test execution. Using it with parallel test execution is unsupported and may have unintended side effects.

Example:

 @Testcontainers
 class MyTestcontainersTests {

     // will be shared between test methods
     @Container
     private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer();

     // will be started before and stopped after each test method
     @Container
     private PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer()
             .withDatabaseName("foo")
             .withUsername("foo")
             .withPassword("secret");

     @Test
     void test() {
         assertTrue(MY_SQL_CONTAINER.isRunning());
         assertTrue(postgresqlContainer.isRunning());
     }
 }
 
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether tests should be disabled (rather than failing) when Docker is not available.
    boolean
    Whether containers should start in parallel.
  • Element Details

    • disabledWithoutDocker

      boolean disabledWithoutDocker
      Whether tests should be disabled (rather than failing) when Docker is not available. Defaults to false.
      Returns:
      if the tests should be disabled when Docker is not available
      Default:
      false
    • parallel

      boolean parallel
      Whether containers should start in parallel. Defaults to false.
      Returns:
      if the containers should start in parallel
      Default:
      false