按照此处的建议,我正在尝试使用嵌入式kafka测试我的春季启动流应用程序。
但是,简单地创建给定的配置
@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
,则问题消失了,但是实际测试的实际豆无法自动。
我自己没有定义StreamBuilderFactoryBean
,它们来自哪里?
另外:这个设置是否值得测试用于喂食后来查询的KTable
的流?这并不是我可以"为每个测试使用另一个主题",因为该流将始终使用同一主题。我希望我能以适当的测试案例设计来解决这个问题,还是我要撞到我看不到的墙?
根据您的堆栈跟踪,您还将使用kafka streams binder使用弹簧云流。请添加一个适当的标签。
考虑删除明确的@EnableKafkaStreams
,因为活页夹会为您照顾基础架构。