the Spring-Kafka in memory Server is started with a random port. As such the application.yml entry is a variable:
bootstrap-servers: ${spring.embedded.kafka.brokers}
However, this property is only set when the embedded Kafka server is actually run. In unit tests without the embedded Kafka exceptions are thrown (because the variable is not actually being set):
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'kafkaReceiverConfig': Injection of
autowired dependencies failed; nested exception is
java.lang.IllegalArgumentException: Could not resolve placeholder
spring.embedded.kafka.brokers' in value "${spring.embedded.kafka.brokers}"
Here is the Java configuration class:
@Configuration
@EnableKafka
public class KafkaReceiverConfig {
@Value("${kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public KafkaReceiver kafkaReceiver() {
return new KafkaReceiver();
}
}
Inspiration is taken from here.
Workaround for now is to include the embedded Kafka into every unit test )-;
How can such drastic measures be avoided ?