Skip to content

Azure Module

Note

This module is INCUBATING. While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. See our contributing guidelines for more information on our incubating modules policy.

Testcontainers module for the Microsoft Azure's SDK.

Currently, the module supports CosmosDB emulator. In order to use it, you should use the following class:

Class Container Image
CosmosDBEmulatorContainer mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator

Usage example

CosmosDB

Start Azure CosmosDB Emulator during a test:

public CosmosDBEmulatorContainer emulator = new CosmosDBEmulatorContainer(
    DockerImageName.parse("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest")
);

Prepare KeyStore to use for SSL.

Path keyStoreFile = tempFolder.newFile("azure-cosmos-emulator.keystore").toPath();
KeyStore keyStore = emulator.buildNewKeyStore();
keyStore.store(new FileOutputStream(keyStoreFile.toFile()), emulator.getEmulatorKey().toCharArray());

Set system trust-store parameters to use already built KeyStore:

System.setProperty("javax.net.ssl.trustStore", keyStoreFile.toString());
System.setProperty("javax.net.ssl.trustStorePassword", emulator.getEmulatorKey());
System.setProperty("javax.net.ssl.trustStoreType", "PKCS12");

Build Azure CosmosDB client:

CosmosAsyncClient client = new CosmosClientBuilder()
    .gatewayMode()
    .endpointDiscoveryEnabled(false)
    .endpoint(emulator.getEmulatorEndpoint())
    .key(emulator.getEmulatorKey())
    .buildAsyncClient();

Test against the Emulator:

CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists("Azure").block();
assertThat(databaseResponse.getStatusCode()).isEqualTo(201);
CosmosContainerResponse containerResponse = client
    .getDatabase("Azure")
    .createContainerIfNotExists("ServiceContainer", "/name")
    .block();
assertThat(containerResponse.getStatusCode()).isEqualTo(201);

Adding this module to your project dependencies

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

testImplementation "org.testcontainers:azure:1.20.4"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>azure</artifactId>
    <version>1.20.4</version>
    <scope>test</scope>
</dependency>