gpt4 book ai didi

java - 如何在 @ContextConfiguration 初始值设定项之前启动 kafka 测试容器?

转载 作者:行者123 更新时间:2023-11-30 09:59:54 26 4
gpt4 key购买 nike

我想启动kafka测试容器并获取它的引导服务器:

@SpringBootTest
@ContextConfiguration(classes = {TestConfig.class, MyApplication.class}, initializers = MyIntegrationTest.Initializer.class)
@Testcontainers
public class MyIntegrationTest {

@Container
private static final KafkaContainer KAFKA = new KafkaContainer();

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(@NotNull ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues values = TestPropertyValues.of(
"spring.kafka.consumer.bootstrap-servers=" + KAFKA.getBootstrapServers(),
"spring.producer.bootstrap-servers=" + KAFKA.getBootstrapServers()
);
values.applyTo(configurableApplicationContext);
}
}

不幸的是我得到:

java.lang.IllegalStateException: You should start Kafka container first
at org.testcontainers.containers.KafkaContainer.getBootstrapServers(KafkaContainer.java:65) ~[kafka-1.12.2.jar:na]

最佳答案

唯一的问题是调用对象后调用start()方法

private static void startKafkaContainer() {

KafkaContainer kafkaContainer = new KafkaContainer();
kafkaContainer.start();

}

更多信息请查看此repository ,请。

关于java - 如何在 @ContextConfiguration 初始值设定项之前启动 kafka 测试容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com