Aracılığıyla paylaş


Testcontainers için Spring Cloud Azure desteği

Bu makalede, uygulamalarınız için etkili tümleştirme testleri yazmak için Spring Cloud Azure'ı Testcontainers ile tümleştirme açıklanmaktadır.

Testcontainer ; veritabanlarının, ileti aracılarının, web tarayıcılarının veya docker kapsayıcısında çalıştırabileceğiniz hemen hemen her şeyin basit örneklerini sağlamaya yönelik açık kaynak bir çerçevedir. JUnit ile tümleştirildiğinden, testlerden herhangi biri çalışmadan önce kapsayıcı başlatabilecek bir test sınıfı yazmanızı sağlar. Testcontainer, gerçek bir arka uç hizmetiyle konuşan tümleştirme testleri yazmak için özellikle yararlıdır.

spring-cloud-azure-testcontainers kitaplığı artık aşağıdaki Azure hizmetleri için tümleştirme testini destekliyor:

Hizmet bağlantıları

Hizmet bağlantısı, herhangi bir uzak hizmete bağlantıdır. Spring Boot'un otomatik yapılandırması, bir hizmet bağlantısının ayrıntılarını tüketebilir ve bunları kullanarak uzak bir hizmete bağlantı kurabilir. Bunu yaparken bağlantı ayrıntıları, bağlantıyla ilgili yapılandırma özelliklerine göre önceliklidir.

Testcontainers kullandığınızda, test sınıfındaki kapsayıcı alanına ek açıklama ekleyerek kapsayıcıda çalışan bir hizmet için bağlantı ayrıntılarını otomatik olarak oluşturabilirsiniz.

xxxContainerConnectionDetailsFactory sınıfları ile spring.factorieskaydedilir. Bu fabrikalar, belirli bir ConnectionDetails alt sınıfını veya Docker görüntü adını temel alan bir Container çekirdeği oluşturur.

Aşağıdaki tabloda, spring-cloud-azure-testcontainers JAR'da desteklenen bağlantı ayrıntıları fabrika sınıfları hakkında bilgi sağlanır:

Bağlantı ayrıntıları fabrika sınıfı Bağlantı ayrıntıları çekirdeği
CosmosContainerConnectionDetailsFactory AzureCosmosConnectionDetails
StorageBlobContainerConnectionDetailsFactory AzureStorageBlobConnectionDetails
StorageQueueContainerConnectionDetailsFactory AzureStorageQueueConnectionDetails
EventHubsContainerConnectionDetailsFactory AzureEventHubsConnectionDetails
ServiceBusContainerConnectionDetailsFactory AzureServiceBusConnectionDetails

Bağımlılıkları ayarlama

Aşağıdaki yapılandırma gerekli bağımlılıkları ayarlar:

  <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>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>testcontainers-junit-jupiter</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-testcontainers</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-starter-cosmos</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

Testcontainers kullanma

Aşağıdaki kod örneği, Testcontainers'ın temel kullanımını gösterir:

@SpringBootTest(classes = CosmosTestcontainersTest.class)
@Testcontainers
@ExtendWith(SpringExtension.class)
@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"))
                .waitingFor(Wait.forHttps("/_explorer/emulator.pem").forStatusCode(200).allowInsecure())
                .withStartupTimeout(Duration.ofMinutes(3));

    @BeforeAll
    public static void setup() throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
        Path keyStoreFile = new File(tempFolder, "azure-cosmos-emulator.keystore").toPath();
        KeyStore keyStore = cosmos.buildNewKeyStore();
        try (var out = Files.newOutputStream(keyStoreFile.toFile().toPath())) {
            keyStore.store(out, cosmos.getEmulatorKey().toCharArray());
        }

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

    @Test
    public void test() {
        CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists("Azure");
        assertThat(databaseResponse.getStatusCode()).isEqualTo(201);
        CosmosContainerResponse containerResponse = client
            .getDatabase("Azure")
            .createContainerIfNotExists("ServiceContainer", "/name");
        assertThat(containerResponse.getStatusCode()).isEqualTo(201);
    }

}

kullanmak CosmosDBEmulatorContaineriçin TLS/SSL için bir KeyStore hazırlamanız gerekir. Daha fazla bilgi için Testcontainers belgelerindeki Cosmos DB Azure Modülü bakın. @ServiceConnectionile bu yapılandırma, uygulamadaki Cosmos DB ile ilgili çekirdeklerin Testcontainers tarafından yönetilen Docker kapsayıcısında çalışan Cosmos DB ile iletişim kurmasını sağlar. Bu kurulum, Cosmos DB otomatik yapılandırmasının bağlantıyla ilgili yapılandırma özelliklerini geçersiz kılmak için kullandığı bir AzureCosmosConnectionDetails çekirdeği otomatik olarak tanımlar.

Örnekleri

Daha fazla bilgi için spring-cloud-azure-testcontainers örneklerine bakın.