I am doing integration testing with KafkaContainer; after starting the container, the port is chosen randomly.
I can get the port by container.getBootStrapServer(), but what I need is to put the dynamic host+port in the properties (application-itest.yml, under spring.kafka.bootstrap-servers, because that is where Spring Kafka picks the value to configure my bean of KafkaTemplate).
Or, if I can set the port to be a static value while starting KafkaContainer, like 9999, and set it in the yaml file, it also will work. I don't know how to do neither.(Do I need to inject another bean of KafkaTemplate after getting the value?)
How do I start KafkaContainer:
public static final KafkaContainer kafka = new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka:5.4.3")
);
private AdminClient adminClient;
...
@BeforeEach
void setup() {
if (adminClient == null) {
kafka.start();
// here value is dynamic and is not accessible in Environment; not as `@AutoConfigureWiremock`, I have `wiremock.server` value set after)
String bootStrapServer = kafka.getBootstrapServers();
log.info("Kafka container created at: {}", bootStrapServer);
adminClient = AdminClient.create(Map.of(
AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootStrapServer
));
// create my topic
log.info("Current topic: {}", topic);
adminClient.createTopics(Set.of(
new NewTopic(topic, 4, (short) 1)
));
}
}