首页 > 解决方案 > 使用 Spring Boot 2.1.12 和 Spring for Apache Kafka 2.2.12 进行 JUnit 测试

问题描述

只是想找出一个简单的例子,使用 Spring Boot 2.1.12 和 Spring for Apache Kafka 2.2.12 与 KafkaListener 一起工作,以重试最后一条失败的消息。如果消息失败,则应将消息重定向到将进行重试的另一个主题。我们将有 4 个主题。topic, retryTopic, sucessTopic 和 errorTopic 如果 topic 失败,应该重定向到 retryTopic,在那里进行 3 次重试尝试。如果这些尝试失败,则必须重定向到 errorTopic。如果主题和重试主题都成功,则应重定向到成功主题。我需要用JUnit Test 覆盖 90% 的案例。

标签: spring-kafkaspring-retryspring-kafka-test

解决方案


public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    // this package must match the package in the <generatePackage>
    // specified in
    // pom.xml
    marshaller.setContextPath("br.com.company.ws");
    return marshaller;
}

@Bean
public CountryClient countryClient(Jaxb2Marshaller marshaller) {
    CountryClient client = new CountryClient();
    client.setDefaultUri(link);
    WebServiceTemplate template = client.getWebServiceTemplate();
    template.setMessageSender(new WebServiceMessageSenderWithAuth(username, password));
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    return client;
}

@Service 公共类 CountryClient 扩展 WebServiceGatewaySupport {

@Value("${spring.link.consumer.link}")
private String link;


public ZfifNfMaoResponse getCountry(ZfifNfMao zfifNfMao) {

    zfifNfMao = new ZfifNfMao();

    ZfifNfMaoResponse response = (ZfifNfMaoResponse)getWebServiceTemplate().marshalSendAndReceive(link, zfifNfMao);

    return response;
}

}


推荐阅读