Reputation: 26503
I would like to start kafka test container and obtain it's bootstrap servers:
@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);
}
}
Unfortunately I get:
java.lang.IllegalStateException: You should start Kafka container first
at org.testcontainers.containers.KafkaContainer.getBootstrapServers(KafkaContainer.java:65) ~[kafka-1.12.2.jar:na]
Upvotes: 2
Views: 3359
Reputation: 944
The only problem is that to call start()
method after invoking object
private static void startKafkaContainer() {
KafkaContainer kafkaContainer = new KafkaContainer();
kafkaContainer.start();
}
Further information check this repository, please.
Upvotes: 5