Camel unit testing with MockEndPointsandSkip

穿精又带淫゛_ 提交于 2019-12-25 12:03:39

问题


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");

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();

    }

}

回答1:


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

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

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.

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.

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.



来源:https://stackoverflow.com/questions/45241206/camel-unit-testing-with-mockendpointsandskip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!