I'm facing an issue where my Kafka ProducerConfig
is getting an invalid bootstrap.servers
value because my unit test @PropertySource
isn't resolving the spring.embedded.kafka.brokers
property. When I dump my producer config to the logs, I get the following:
acks = 0
batch.size = 10000
bootstrap.servers = [${spring.embedded.kafka.brokers}]
...
Clearly, the property isn't getting resolved. Suppose I have the following embedded Kafka test.
@EmbeddedKafka
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@RunWith(SpringRunner.class)
public class EmbeddedKafkaTest {
@Value("${spring.embedded.kafka.brokers}")
private String embeddedKafkaBrokers;
@Test
public void test(){}
@SpringBootConfiguration
@PropertySource("classpath:kafkaTestProps.properties")
@EnableAutoConfiguration
class EmbeddedKafkaTestConfiguration {
}
}
and my kafkaTestProperties.properties
file is as follows:
embedded-kafka-brokers=${spring.embedded.kafka.brokers}
...
embedded-kafka-brokers
will eventually be provided to Kafka's ProducerConfig
through some underlying autoconfiguration.
Interestingly, the embeddedKafkaBrokers
instance field in the test class does contain the broker IPs set by embedded kafka.
I've concluded there's an issue with the property source loading order, where @EmbeddedKafka
isn't setting the broker IP system property in time for kafkaTestProperties.properties
to resolve it. This problem arose after porting our code base to Spring Boot 2 and Spring Cloud Finchley SR2, where Spring Kafka APIs have upgraded.
I've tried removing @SpringBootTest
and wrapping the test()
code in a SpringApplicationBuilder
to no avail.
Any advice as to how I can fix this? Perhaps I can leverage @AutoConfigurationAfter
or @Order
? Is there a way to order the loading of property sources?