26 Aralık 2022 Pazartesi

Testcontainers ElasticsearchContainer Sınıfı

Gradle
Şu satırı dahil ederiz
dependencies {
...
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.testcontainers:elasticsearch:1.17.4'
testImplementation "org.testcontainers:testcontainers:1.17.5"
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.junit.jupiter:junit-jupiter'
}

dependencyManagement {
imports {
mavenBom "org.testcontainers:testcontainers-bom:${testcontainersVersion}"
mavenBom "org.junit:junit-bom:5.8.1"
}
}
Örnek
Elimizde şöyle bir kod olsun
public class ElasticTestContainer extends ElasticsearchContainer {
  private static final String DOCKER_ELASTIC = 
    "docker.elastic.co/elasticsearch/elasticsearch:7.17.6";

  private static final String CLUSTER_NAME = "sample-cluster";

  private static final String ELASTIC_SEARCH = "elasticsearch";

  public ElasticTestContainer() {
    super(DOCKER_ELASTIC);
    this.addFixedExposedPort(9200, 9200);
    this.addFixedExposedPort(9300, 9300);
    this.addEnv(CLUSTER_NAME, ELASTIC_SEARCH);
  }
}
Test kodunda şöyle yaparız
// using the above test container using @container annotation
@Container
protected static ElasticsearchContainer elasticsearchContainer =
  new ElasticTestContainer();




Hiç yorum yok:

Yorum Gönder