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

时间:2017-02-16 21:01:15

标签: apache-kafka spring-cloud-stream

我不知道如何使用kafka进行一个样本测试,我试着按照春季指南但不行。

有人能帮助我吗?

zzzzz zz z z z z z z z z z z z

@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;

3 个答案:

答案 0 :(得分:0)

看起来您在类路径上设置了不匹配的依赖项(即旧版本的Spring Cloud Stream核心)。

您可以通过删除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活页夹进行测试 - 它还显示了如何在同一个应用程序中使用两个不同的活页夹,但可能您并不关心它。)

答案 1 :(得分:0)

Marius Bogoevici,我的依赖者

public selectedI(selected) {
    this.selectedII = selected;
}

selectedII: any; //the variable I'm trying to store it into

答案 2 :(得分:0)

这是因为Marius上面指出的版本不兼容。

您需要Camden.SR5具有兼容版本的Spring Cloud Stream和Spring Cloud Stream测试支持,或Camden.SR4具有Spring Cloud Stream测试支持版本1.1.0.RELEASE

This是Spring Cloud Steram 1.1.0.RELEASE1.1.1.RELEASE之间的变化: