gpt4 book ai didi

spring-boot - EmbeddedKafka kafka streams 用SpringBootTest测试发现两个StreamsBuilderFactoryBeans

转载 作者:行者123 更新时间:2023-12-04 13:19:11 27 4
gpt4 key购买 nike

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 将无法 Autowiring 。

我自己没有定义StreamBuilderFactoryBean,它们从哪里来?

此外:这个设置是否值得测试用于提供稍后被查询的 KTable 的流?这不像我可以“为每个测试使用不同的主题”,因为流将始终使用相同的主题。我希望我可以通过适当的测试用例设计来解决这个问题,否则我会撞到我还看不到的墙吗?

最佳答案

根据您的堆栈跟踪,您还使用了 Spring Cloud Stream 和 Kafka Streams Binder。请添加适当的标签。

考虑删除显式 @EnableKafkaStreams,因为 Binder 会为您处理基础设施。

关于spring-boot - EmbeddedKafka kafka streams 用SpringBootTest测试发现两个StreamsBuilderFactoryBeans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56003225/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com