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 Azurite, Azure Event Hubs, Azure Service Bus and CosmosDB emulators. In order to use them, you should use the following classes:

Class Container Image
AzuriteContainer mcr.microsoft.com/azure-storage/azurite
EventHubsEmulatorContainer mcr.microsoft.com/azure-messaging/eventhubs-emulator
ServiceBusEmulatorContainer mcr.microsoft.com/azure-messaging/servicebus-emulator
CosmosDBEmulatorContainer mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator

Usage example

Azurite Storage Emulator

Start Azurite Emulator during a test:

AzuriteContainer emulator = new AzuriteContainer("mcr.microsoft.com/azure-storage/azurite:3.33.0")

Note

SSL configuration is possible using the withSsl(MountableFile, String) and withSsl(MountableFile, MountableFile) methods.

Newer Azure Storage SDK versions can send API versions that Azurite does not support. Use withCommandOptions(...) to append extra Azurite flags such as --skipApiVersionCheck. AzuriteContainer rebuilds its process command in configure(), so .withCommand(...) cannot be used for extra flags.

AzuriteContainer emulator = new AzuriteContainer("mcr.microsoft.com/azure-storage/azurite:3.33.0")
    .withCommandOptions("--skipApiVersionCheck");

If the tested application needs to use more than one set of credentials, the container can be configured to use custom credentials. Please see some examples below.

AzuriteContainer emulator = new AzuriteContainer("mcr.microsoft.com/azure-storage/azurite:3.33.0")
    .withEnv("AZURITE_ACCOUNTS", "account1:key1:key2")
AzuriteContainer emulator = new AzuriteContainer("mcr.microsoft.com/azure-storage/azurite:3.33.0")
    .withEnv("AZURITE_ACCOUNTS", "account1:key1;account2:key2")

Using with Blob

Build Azure Blob client:

BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
    .connectionString(container.getConnectionString())
    .buildClient();

In case the application needs to use custom credentials, we can obtain them with a different method:

String connectionString1 = emulator.getConnectionString("account1", "key1");
// the second account will not have access to the same container
String connectionString2 = emulator.getConnectionString("account2", "key2");

Using with Queue

Build Azure Queue client:

QueueServiceClient queueServiceClient = new QueueServiceClientBuilder()
    .connectionString(container.getConnectionString())
    .buildClient();

Note

We can use custom credentials the same way as defined in the Blob section.

Using with Table

Build Azure Table client:

TableServiceClient tableServiceClient = new TableServiceClientBuilder()
    .connectionString(container.getConnectionString())
    .buildClient();

Note

We can use custom credentials the same way as defined in the Blob section.

Azure Event Hubs Emulator

{
    "UserConfig": {
        "NamespaceConfig": [
            {
                "Type": "EventHub",
                "Name": "emulatorNs1",
                "Entities": [
                    {
                        "Name": "eh1",
                        "PartitionCount": "1",
                        "ConsumerGroups": [
                            {
                                "Name": "cg1"
                            }
                        ]
                    }
                ]
            }
        ],
        "LoggingConfig": {
            "Type": "File"
        }
    }
}

Start Azure Event Hubs Emulator during a test:

Network network = Network.newNetwork();
AzuriteContainer azuriteContainer = new AzuriteContainer("mcr.microsoft.com/azure-storage/azurite:3.33.0")
    .withNetwork(network);
EventHubsEmulatorContainer emulator = new EventHubsEmulatorContainer(
    "mcr.microsoft.com/azure-messaging/eventhubs-emulator:2.0.1"
)
    .acceptLicense()
    .withNetwork(network)
    .withConfig(MountableFile.forClasspathResource("/eventhubs_config.json"))
    .withAzuriteContainer(azuriteContainer);

Using Azure Event Hubs clients

Configure the consumer and the producer clients:

EventHubProducerClient producer = new EventHubClientBuilder()
    .connectionString(emulator.getConnectionString())
    .fullyQualifiedNamespace("emulatorNs1")
    .eventHubName("eh1")
    .buildProducerClient();
EventHubConsumerClient consumer = new EventHubClientBuilder()
    .connectionString(emulator.getConnectionString())
    .fullyQualifiedNamespace("emulatorNs1")
    .eventHubName("eh1")
    .consumerGroup("cg1")
    .buildConsumerClient();

Azure Service Bus Emulator

{
    "UserConfig": {
        "Namespaces": [
            {
                "Name": "sbemulatorns",
                "Queues": [
                    {
                        "Name": "queue.1",
                        "Properties": {
                            "DeadLetteringOnMessageExpiration": false,
                            "DefaultMessageTimeToLive": "PT1H",
                            "DuplicateDetectionHistoryTimeWindow": "PT20S",
                            "ForwardDeadLetteredMessagesTo": "",
                            "ForwardTo": "",
                            "LockDuration": "PT1M",
                            "MaxDeliveryCount": 3,
                            "RequiresDuplicateDetection": false,
                            "RequiresSession":