使用kafka使用spring云流创建单元测试时出错

ou6hu8tu  于 2021-06-07  发布在  Kafka
关注(0)|答案(3)|浏览(467)

我不知道如何使一个样本测试使用Kafka,我试图遵循 Spring 指南,但没有工作。
有人能帮我吗?
滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋滋

@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext 
public class EnrollSenderTest {

@Autowired
public EnrollSender producer;

@Autowired
private BinderFactory<MessageChannel> binderFactory;

@Autowired
private MessageCollector messageCollector;

@SuppressWarnings("unchecked")
@Test
public void test() {

    Message<String> message = new GenericMessage<>("hello");
    producer.sendEnroll(message);

    Message<String> received = (Message<String>) messageCollector.forChannel(producer.getOutput()).poll();
    assertThat(received.getPayload(), equalTo("hello"));
}
}

我的班主任是:

@Service
@EnableBinding(Source.class)
public class EnrollSender {

private final MessageChannel output;

public EnrollSender(Source output) {
    this.output = output.output();
}

public void sendEnroll(Object enroll) {
    output.send(MessageBuilder.withPayload(enroll).build());
}

public MessageChannel getOutput() {
    return output;
}

}
但给出以下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageCollector' defined in class path resource [org/springframework/cloud/stream/test/binder/TestSupportBinderAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.stream.test.binder.MessageCollector]: Factory method 'messageCollector' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.cloud.stream.binder.BinderFactory.getBinder(Ljava/lang/String;Ljava/lang/Class;)Lorg/springframework/cloud/stream/binder/Binder;

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.stream.test.binder.MessageCollector]: Factory method 'messageCollector' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.cloud.stream.binder.BinderFactory.getBinder(Ljava/lang/String;Ljava/lang/Class;)Lorg/springframework/cloud/stream/binder/Binder;

Caused by: java.lang.NoSuchMethodError: org.springframework.cloud.stream.binder.BinderFactory.getBinder(Ljava/lang/String;Ljava/lang/Class;)Lorg/springframework/cloud/stream/binder/Binder;
webghufk

webghufk1#

看起来您在类路径上有一个不匹配的依赖项集(即旧版本的spring cloud stream core)。
您可以通过删除的版本来解决此问题 spring-cloud-stream-test-support 因为 Camden.SR4 bom将提供正确的。
此外,如果要使用嵌入式kafka示例进行测试,可以在下面找到一个示例:https://github.com/spring-cloud/spring-cloud-stream-samples/blob/master/multibinder/src/test/java/multibinder/rabbitandkafkabinderapplicationtests.java#l57
(该示例向您展示了如何使用嵌入式代理配置kafka活页夹进行测试—它还展示了如何在同一个应用程序中使用两个不同的活页夹,但您可能并不关心这一点)。

68de4m5k

68de4m5k2#

马吕斯·博戈维奇,我的家人

dependencyManagement {
 imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR4"
   }
}

compile 'org.springframework.cloud:spring-cloud-starter-stream-kafka'
compile group: 'org.springframework.cloud', name: 'spring-cloud-stream-test-support', version: '1.1.1.RELEASE'
nimxete2

nimxete23#

这是因为马吕斯在上面指出了不兼容的版本。
你要么需要 Camden.SR5 具有兼容版本的spring cloud stream和spring cloud stream测试支持或 Camden.SR4 支持spring云流测试版本 1.1.0.RELEASE .
这是介于两者之间的变化 1.1.0.RELEASE 以及 1.1.1.RELEASE Spring的云彩:

相关问题