使用MockEndPointsandSkip进行骆驼单元测试 [英] Camel unit testing with MockEndPointsandSkip

查看:110
本文介绍了使用MockEndPointsandSkip进行骆驼单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是Camel的新手,正在尝试一些代码以了解MockEndpoints功能如何工作,但无法正常工作.这是我要使用MockEndpoints进行单元测试的骆驼路线.属性值在application.properties文件中定义.

Am a Camel newbie and am trying out some code to understand how the MockEndpoints functionality works, but it is not working. Here are my Camel routes that I want to unit test using MockEndpoints. The property values are defined in the application.properties file.

from("kafka:{{kafka.servers}}?topic={{kafka.consumer.topic}}&groupId={{kafka.consumer.groupId}}")
    .to("direct:notify");

from("direct:notify")
    .log("direct:notfy");

这是我的测试代码,未通过确认接收到1个Exchange.似乎生产者从未将邮件发送到我的Kafka Mockendpoint:

Here is my Test code which is failing the assertion of receiving 1 Exchange. It appears that the Producer is never sending the message to my Kafka Mockendpoint:

@RunWith(SpringRunner.class)
@SpringBootTest
@MockEndpointsAndSkip("kafka:{{kafka.servers}}?*|direct:notify")
public class NotificationApplicationTests {     

    @Autowired
    private CamelContext context;

    @Value("${kafka.servers}")
    private String kafkaServers;

    @EndpointInject(uri="mock:kafka:{{kafka.servers}}")
    private MockEndpoint mockKafka;

    @EndpointInject(uri="mock:direct:notify")
    private MockEndpoint mockDirect;

    @Autowired
    private ProducerTemplate notiProducer;

    @Test
    public void testMockEndpoints() throws Exception{                                       

        mockDirect.setExpectedMessageCount(1);
        mockDirect.whenAnyExchangeReceived( (Exchange exchange) -> {
            //NEVER GETS HERE!!
            log.info("MOCK DIRECT exchange received");          
        });     

        String payload = "{'email': 'test@gmail.com', 'data': 'ABC1234'}";

        notiProducer.sendBody(mockKafka, payload);

        mockDirect.assertIsSatisfied();

    }

}

推荐答案

如果您希望模拟端点是正则表达式,则您的模拟端点是错误的.

Your mock endpoint is wrong if you expect it to be a regular expression.

@MockEndpointsAndSkip("kafka:{{kafka.servers}}?*|direct:notify")

请注意在正则表达式中使用*.由于它不是通配符,因此通常需要使用.*来匹配0..n个字符.进一步了解正则表达式,也许可以使用正则表达式测试器,您可以在网上找到它,也可以将其安装为Java编辑器中的插件.

Take care about using * in a regular expression. As its not a wildcard, you typically would need to use .* to match 0..n characters. Look at bit more on regular expression and maybe use a regular expression tester which you can find online or install as plugin in your Java editor.

此外,如果您要替换以kafka开头的路线,则还需要做其他事情,因为@MockEndpointsAndSkip并不是开头.但是用于发送.请参阅建议与测试"部分中有关replaceFrom的信息: http://camel.apache.org/advicewith .html .

Also if you want to replace the route starting with kafka, then you need to do something else as @MockEndpointsAndSkip is not for the start. But for sending. See about replaceFrom in the advice-with testing section: http://camel.apache.org/advicewith.html.

我的《骆驼在行动》第二版在整个测试章节中都有很多文档: https://www.manning.com/books/camel-in-action-second-edition ,如果您正在寻找出色的Camel文档.

And my book Camel in Action 2nd edition has a lot more documentation in the entire testing chapter: https://www.manning.com/books/camel-in-action-second-edition if you are looking for great Camel documentation.

这篇关于使用MockEndPointsandSkip进行骆驼单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆