Skip to content

Kafka autoconfiguration fails to resolve spring.kafka.bootstrap-servers when using Testcontainers' KafkaContainer #34770

@jucosorin

Description

@jucosorin

Hello.

I have a Spring Boot project that uses Testcontainers Kafka for integration tests. I use the following to add the Kafka container to the @SpringBootTest:

@Container
private static final KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse(TESTCONTAINERS_KAFKA_IMAGE))
      .withEnv("KAFKA_AUTO_CREATE_TOPICS_ENABLE", "FALSE");

In order to inject the spring.kafka.bootstrap-servers I use @DynamicPropertySource like:

@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
  registry.add("spring.kafka.bootstrap-servers", kafkaContainer::getBootstrapServers);
}

Since upgrading to 3.1.0-M2, injecting the spring.kafka.bootstrap-servers fails and the root exception is:

Caused by: java.lang.NumberFormatException: For input string: "//localhost:54456"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
	at java.base/java.lang.Integer.parseInt(Integer.java:654)
	at java.base/java.lang.Integer.parseInt(Integer.java:786)
	at org.springframework.boot.autoconfigure.kafka.PropertiesKafkaConnectionDetails.asNode(PropertiesKafkaConnectionDetails.java:72)

Debugging this I've tracked this down to org.springframework.boot.autoconfigure.kafka.PropertiesKafkaConnectionDetails and the following method:

private Node asNode(String bootstrapNode) {
  int separatorIndex = bootstrapNode.indexOf(':');
  if (separatorIndex == -1) {
    return new Node(bootstrapNode, this.DEFAULT_PORT);
  }
    return new Node(bootstrapNode.substring(0, separatorIndex),
      Integer.parseInt(bootstrapNode.substring(separatorIndex + 1)));
}

This method expects a bootstrapNode string like "localhost:9092", but KafkaContainer::getBootstrapServers() sends it "PLAINTEXT://localhost:9092".

@wilkinsona I see that this class was introduced recently. If you agree that this is a bug, I could look into it and try to resolve it.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions