Edit

Share via


Spring Cloud Azure support for Docker Compose

This article describes how to integrate Spring Cloud Azure with Docker Compose to write effective integration tests for your applications.

Docker Compose is a tool for defining and running multi-container applications. It's the key to unlocking a streamlined and efficient development and deployment experience.

The spring-cloud-azure-docker-compose library now supports integration testing for the following Azure services:

Service connections

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 Docker compose, you can automatically create connection details for a service running in a container by adding the @SpringBootTest annotation with the spring.docker.compose.file property in the test class.

The xxxDockerComposeConnectionDetailsFactory classes are registered with spring.factories. These factories create a ConnectionDetails bean based on a DockerComposeConnectionDetails.

The following table provides information about the connection details factory classes supported in the spring-cloud-azure-docker-compose JAR:

Connection details factory class Connection details bean
StorageBlobDockerComposeConnectionDetailsFactory AzureStorageBlobConnectionDetails
StorageQueueDockerComposeConnectionDetailsFactory AzureStorageQueueConnectionDetails
EventHubsDockerComposeConnectionDetailsFactory AzureEventHubsConnectionDetails
ServiceBusDockerComposeConnectionDetailsFactory AzureServiceBusConnectionDetails

Set up dependencies

The following configuration sets up the required dependencies:

  <properties>
    <version.spring.cloud.azure>7.1.0</version.spring.cloud.azure>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-dependencies</artifactId>
        <version>${version.spring.cloud.azure}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-starter-storage-blob</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-docker-compose</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

Create test resource files

Create these files in the src/test/resources folder:

storage-compose.yaml:

services:
  storage:
    image: mcr.microsoft.com/azure-storage/azurite:latest
    ports:
      - '10000'
      - '10001'
      - '10002'
    command: azurite -l /data --blobHost 0.0.0.0 --queueHost 0.0.0.0 --tableHost 0.0.0.0 --skipApiVersionCheck

Create Java codes

The following code example demonstrates the basic usage of Docker compose:

@SpringBootTest(properties = {
        "spring.docker.compose.skip.in-tests=false",
        "spring.docker.compose.file=classpath:storage-compose.yaml",
        "spring.docker.compose.stop.command=down"
})
public class AzureBlobResourceDockerComposeTest {

    @Value("azure-blob://testcontainers/message.txt")
    private Resource blobFile;

    @Test
    void blobResourceShouldWriteAndReadContent() throws IOException {
        String originalContent = "Hello World!";
        try (OutputStream os = ((WritableResource) this.blobFile).getOutputStream()) {
            os.write(originalContent.getBytes());
        }
        String resultContent = StreamUtils.copyToString(this.blobFile.getInputStream(), Charset.defaultCharset());
        assertThat(resultContent).isEqualTo(originalContent);
    }

    @Configuration(proxyBeanMethods = false)
    @ImportAutoConfiguration(classes = {
            AzureGlobalPropertiesAutoConfiguration.class,
            AzureStorageBlobAutoConfiguration.class,
            AzureStorageBlobResourceAutoConfiguration.class})
    static class Config {
    }
}

With spring.docker.compose.file, this configuration enables related beans in the app to communicate with Blob Storage running inside the Docker container. This action is done by automatically defining a AzureStorageBlobConnectionDetails bean, which is then used by the Blob Storage autoconfiguration, overriding any connection-related configuration properties.

Samples

For more information, see the spring-cloud-azure-docker-compose examples.