在advice here之后,我正在尝试使用嵌入式Kafka测试我的Spring Boot Streams应用程序。
但是,只需创建给定的配置
@Configuration
@EnableKafkaStreams
public class StreamsTestConfiguration {
@Value("${" + EmbeddedKafkaBroker.SPRING_EMBEDDED_KAFKA_BROKERS + "}")
private String brokerAddresses;
@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
public KafkaStreamsConfiguration kStreamsConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "testStreams");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, this.brokerAddresses);
return new KafkaStreamsConfiguration(props);
}
}
和一个简单的测试
@RunWith(SpringRunner.class)
@SpringBootTest
@EmbeddedKafka(topics = { "topic" })
public class EmbeddedKafkaTest {
@Autowired
private MyBean tested;
@Autowired
private EmbeddedKafkaBroker kafkaBroker;
@Test
public void loaded() {}
}
无法运行:
Parameter 0 of method kafkaStreamsFactoryBeanConfigurer in org.springframework.boot.autoconfigure.kafka.KafkaStreamsAnnotationDrivenConfiguration required a single bean, but 2 were found:
- &defaultKafkaStreamsBuilder: defined by method 'defaultKafkaStreamsBuilder' in class path resource [org/springframework/kafka/annotation/KafkaStreamsDefaultConfiguration.class]
- &stream-builder-process: defined in null
[...]
Caused by: org.springframework.context.ApplicationContextException: Failed to start bean 'outputBindingLifecycle'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'kafkaStreamsFactoryBeanConfigurer' defined in org.springframework.boot.autoconfigure.kafka.KafkaStreamsAnnotationDrivenConfiguration: Unsatisfied dependency expressed through method 'kafkaStreamsFactoryBeanConfigurer' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.kafka.config.StreamsBuilderFactoryBean' available: expected single matching bean but found 2: &defaultKafkaStreamsBuilder,&stream-builder-process
如果我从测试类中删除@SpringBootTest
,问题就消失了,但是然后实际的被测bean无法自动装配。
我自己没有定义StreamBuilderFactoryBean
,它们来自哪里?
此外:此设置是否值得测试用于馈送KTable
的流,该流随后将被查询?好像我不能“为每个测试使用不同的主题”,因为流将始终使用相同的主题。我希望我可以通过适当的测试用例设计解决这个问题,还是要撞到我看不见的墙?
答案 0 :(得分:3)
根据堆栈跟踪,您还可以将Spring Cloud Stream与Kafka Streams Binder一起使用。请添加适当的标签。
请考虑删除明确的@EnableKafkaStreams
,因为Binder会为您处理基础结构。