Skip to content

InfluxDB Module

Testcontainers module for InfluxData InfluxDB.

Important note

There are breaking changes in InfluxDB 2.x. For more information refer to the main documentation. You can find more information about the official InfluxDB image on Docker Hub.

InfluxDB 2.x usage example

Running a InfluxDBContainer as a stand-in for InfluxDB in a test:

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:2.0.7")
)

The InfluxDB instance will be setup with the following data:

Property Default Value
username test-user
password test-password
organization test-org
bucket test-bucket
retention 0 (infinite)
adminToken -

For more details about the InfluxDB setup, please visit the official InfluxDB documentation.

It is possible to overwrite the default property values. Create a container with InfluxDB admin token:

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:2.0.7")
)
    .withAdminToken(ADMIN_TOKEN)

Or create a container with custom username, password, bucket, organization, and retention time:

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:2.0.7")
)
    .withUsername(USERNAME)
    .withPassword(PASSWORD)
    .withOrganization(ORG)
    .withBucket(BUCKET)
    .withRetention(RETENTION);

The following code snippet shows how you can create an InfluxDB Java client:

public static InfluxDBClient createClient(final InfluxDBContainer<?> influxDBContainer) {
    final InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions
        .builder()
        .url(influxDBContainer.getUrl())
        .authenticate(influxDBContainer.getUsername(), influxDBContainer.getPassword().toCharArray())
        .bucket(influxDBContainer.getBucket())
        .org(influxDBContainer.getOrganization())
        .build();
    return InfluxDBClientFactory.create(influxDBClientOptions);
}

Hint

You can find the latest documentation about the InfluxDB 2.x Java client here.

InfluxDB 1.x usage example

Running a InfluxDBContainer as a stand-in for InfluxDB in a test with default env variables:

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:1.4.3")
)

The InfluxDB instance will be setup with the following data:

Property Default Value
username test-user
password test-password
authEnabled true
admin admin
adminPassword password
database -

It is possible to overwrite the default values. For instance, creating an InfluxDB container with a custom username, password, and database name:

final InfluxDBContainer<?> influxDBContainer = new InfluxDBContainer<>(
    DockerImageName.parse("influxdb:1.4.3")
)
    .withDatabase(DATABASE)
    .withUsername(USER)
    .withPassword(PASSWORD)

In the following example you will find a snippet to create an InfluxDB client using the official Java client:

public static InfluxDB createInfluxDBWithUrl(final InfluxDBContainer<?> container) {
    InfluxDB influxDB = InfluxDBFactory.connect(
        container.getUrl(),
        container.getUsername(),
        container.getPassword()
    );
    influxDB.setDatabase(container.getDatabase());
    return influxDB;
}

Hint

You can find the latest documentation about the InfluxDB 1.x Java client here.

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

testImplementation "org.testcontainers:influxdb:1.19.7"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>influxdb</artifactId>
    <version>1.19.7</version>
    <scope>test</scope>
</dependency>

Hint

Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency.