Spring Cloud Azure support for Testcontainers
This article applies to: ✔️ Version 5.18.0
This article describes how to integrate Spring Cloud Azure with Testcontainers to write effective integration tests for your applications.
Testcontainers is an open-source framework for providing throwaway, lightweight instances of databases, message brokers, web browsers, or just about anything that can run in a Docker container. It integrates with JUnit, enabling you to write a test class that can start up a container before any of the tests run. Testcontainers is especially useful for writing integration tests that talk to a real backend service.
The spring-cloud-azure-testcontainers
library now supports integration testing for the following Azure services:
A service connection is a connection to any remote service. Spring Boot's autoconfiguration can consume the details of a service connection and use them to establish a connection to a remote service. When doing so, the connection details take precedence over any connection-related configuration properties.
When you use Testcontainers, you can automatically create connection details for a service running in a container by annotating the container field in the test class.
The @ServiceConnection
annotation is processed by xxxContainerConnectionDetailsFactory
classes registered with spring.factories
. These factories create a ConnectionDetails
bean based on a specific Container
subclass or the Docker image name.
The following table provides information about the connection details factory classes supported in the spring-cloud-azure-testcontainers
JAR:
Connection details factory class | Connection details bean |
---|---|
CosmosContainerConnectionDetailsFactory |
AzureCosmosConnectionDetails |
StorageBlobContainerConnectionDetailsFactory |
AzureStorageBlobConnectionDetails |
StorageQueueContainerConnectionDetailsFactory |
AzureStorageQueueConnectionDetails |
The following configuration sets up the required dependencies:
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>azure</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-testcontainers</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-cosmos</artifactId>
</dependency>
The following code example demonstrates the basic usage of Testcontainers:
@SpringBootTest
@Testcontainers
@ImportAutoConfiguration(classes = { AzureGlobalPropertiesAutoConfiguration.class, AzureCosmosAutoConfiguration.class})
public class CosmosTestcontainersTest {
@TempDir
private static File tempFolder;
@Autowired
private CosmosClient client;
@Container
@ServiceConnection
static CosmosDBEmulatorContainer cosmos = new CosmosDBEmulatorContainer(
DockerImageName.parse("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest"));
@BeforeAll
static void setup() {
cosmos.start();
Path keyStoreFile = new File(tempFolder, "azure-cosmos-emulator.keystore").toPath();
KeyStore keyStore = cosmos.buildNewKeyStore();
try {
keyStore.store(Files.newOutputStream(keyStoreFile.toFile().toPath()), cosmos.getEmulatorKey().toCharArray());
} catch (Exception e) {
throw new RuntimeException(e);
}
System.setProperty("javax.net.ssl.trustStore", keyStoreFile.toString());
System.setProperty("javax.net.ssl.trustStorePassword", cosmos.getEmulatorKey());
System.setProperty("javax.net.ssl.trustStoreType", "PKCS12");
}
@Test
void test() {
// ...
}
}
To use CosmosDBEmulatorContainer
, we need to prepare a KeyStore
for TLS/SSL. For more information, see Cosmos DB Azure Module in the Testcontainers documentation. With @ServiceConnection
, this configuration enables Cosmos DB-related beans in the app to communicate with Cosmos DB running inside the Testcontainers-managed Docker container. This action is done by automatically defining a AzureCosmosConnectionDetails
bean, which is then used by the Cosmos DB autoconfiguration, overriding any connection-related configuration properties.
For more information, see the azure-spring-boot-samples repository on GitHub.