testcontainers-extensions

Testcontainers Extensions Minio

Minimum required Java version Maven Central GitHub Action Coverage Maintainability Rating Lines of Code

Testcontainers Minio Extension with advanced testing capabilities.

Features:

Dependency :rocket:

Gradle

testImplementation "io.goodforgod:testcontainers-extensions-minio:0.12.1"

Maven

<dependency>
    <groupId>io.goodforgod</groupId>
    <artifactId>testcontainers-extensions-minio</artifactId>
    <version>0.12.1</version>
    <scope>test</scope>
</dependency>

Content

Usage

Test with container start in PER_RUN mode will look like:

@TestcontainersMinio(mode = ContainerMode.PER_RUN,
        bucket = @Bucket(
                create = Bucket.Mode.PER_METHOD,
                drop = Bucket.Mode.PER_METHOD,
                value = { "my-bucket" }))
class ExampleTests {

  @ConnectionMinio
  private MinioConnection connection;

  @Test
  void test() {
      byte[] bytes = "someValue".getBytes(StandardCharsets.UTF_8);
      connection.client().putObject(PutObjectArgs.builder()
                .bucket("my-bucket")
                .object("someKey")
                .stream(new ByteArrayInputStream(bytes), bytes.length, -1)
                .build());
    }
}

Connection

MinioConnection is an abstraction with asserting data in database container and easily manipulate container connection settings. You can inject connection via @ConnectionMinio as field or method argument or manually create it from container or manual settings.

class ExampleTests {

  @ConnectionMinio 
  private MinioConnection connection;
  
  @Test
  void test() {
    byte[] bytes = "someValue".getBytes(StandardCharsets.UTF_8);
    connection.client().putObject(PutObjectArgs.builder()
            .bucket("my-bucket")
            .object("someKey")
            .stream(new ByteArrayInputStream(bytes), bytes.length, -1)
            .build());
  }
}

Annotation

@TestcontainersMinio - allow automatically start container with specified image in different modes without the need to configure it.

Available containers modes:

Simple example on how to start container per class, no need to configure container:

@TestcontainersMinio(mode = ContainerMode.PER_CLASS)
class ExampleTests {

    @Test
    void test(@ConnectionMinio MinioConnection connection) {
        assertNotNull(connection);
    }
}

That’s all you need.

It is possible to customize image with annotation image parameter.

Image also can be provided from environment variable:

@TestcontainersMinio(image = "${MY_IMAGE_ENV|minio/minio:RELEASE.2024-10-13T13-34-11Z}")
class ExampleTests {

    @Test
    void test() {
        // test
    }
}

Image syntax:

Manual Container

When you need to manually configure container with specific options, you can provide such container as instance that will be used by @TestcontainersMinio, this can be done using @ContainerMinio annotation for container.

Example:

@TestcontainersMinio(mode = ContainerMode.PER_CLASS)
class ExampleTests {

    @ContainerMinio
    private static final MinIOContainer container = new MinIOContainer().withNetworkAliases("myMinio");
    
    @Test
    void test(@ConnectionMinio MinioConnection connection) {
        assertEquals("myMinio", connection.paramsInNetwork().get().host());
    }
}

Network

In case you want to enable Network.SHARED for containers you can do this using network & shared parameter in annotation:

@TestcontainersMinio(network = @Network(shared = true))
class ExampleTests {

    @Test
    void test() {
        // test
    }
}

Default alias will be created by default, even if nothing was specified (depends on implementation).

You can provide also custom alias for container. Alias can be extracted from environment variable also or default value can be provided if environment is missing.

In case specified environment variable is missing default alias will be created:

@TestcontainersMinio(network = @Network(alias = "${MY_ALIAS_ENV|my_default_alias}"))
class ExampleTests {

    @Test
    void test() {
        // test
    }
}

Image syntax:

Annotation Connection

MinioConnection - can be injected to field or method parameter and used to communicate with running container via @ConnectionMinio annotation. MinioConnection provides connection parameters, useful asserts, checks, etc. for easier testing.

Example:

@TestcontainersMinio(mode = ContainerMode.PER_CLASS, image = "minio/minio:RELEASE.2024-10-13T13-34-11Z")
class ExampleTests {

    @ConnectionMinio
    private MinioConnection connection;

    @Test
    void test() {
      connection.client().makeBucket(MakeBucketArgs.builder()
              .bucket("my--test-bucket")
              .build());
    }
}

External Connection

In case you want to use some external Minio instance that is running in CI or other place for tests (due to docker limitations or other), you can use special environment variables and extension will use them to propagate connection and no Minio containers will be running in such case.

Special environment variables:

License

This project licensed under the Apache License 2.0 - see the LICENSE file for details.