简体   繁体   English

Apache Camel Spring Javaconfig单元测试端点上没有使用者

[英]Apache Camel Spring Javaconfig Unit Test No consumers available on endpoint

I have the following route configuration: 我有以下路由配置:

@Component
public class MyRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:in").to("direct:out");
    }
}

When I try to test it: 当我尝试测试时:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyRouteTest.TestConfig.class }, loader = CamelSpringDelegatingTestContextLoader.class)
@MockEndpoints
public class MyRouteTest {
    @EndpointInject(uri = "mock:direct:out")
    private MockEndpoint mockEndpoint;

    @Produce(uri = "direct:in")
    private ProducerTemplate producerTemplate;

    @Configuration
    public static class TestConfig extends SingleRouteCamelConfiguration {
        @Bean
        @Override
        public RouteBuilder route() {
            return new MyRoute();
        }
    }

    @Test
    public void testRoute() throws Exception {
        mockEndpoint.expectedBodiesReceived("Test Message");

        producerTemplate.sendBody("Test Message");

        mockEndpoint.assertIsSatisfied();
    }
}

I get this exception: 我得到这个例外:

org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: Endpoint[direct://out]. org.apache.camel.component.direct.DirectConsumerNotAvailableException:在终结点Endpoint [direct:// out]上没有可用的使用者。 Exchange[Message: Test Message] 交换[消息:测试消息]

It looks like the Mock is not picking up the message from the endpoint. 看起来Mock没有从端点接收消息。

What am I doing wrong? 我究竟做错了什么?

This issue because, in your route configuration, there is no route with "direct:out" consumer endpoint. 出现此问题的原因是,在您的路由配置中,没有使用“ direct:out”使用者端点的路由。

add a line like some thing below, 在下面添加一行,

from("direct:out").("Anything you want to log");

So that direct:out will consume the exchange and In your test, mock will be able check the received text without any issues. 这样,direct:out将消耗交换空间,并且在您的测试中,mock将能够检查收到的文本而不会出现任何问题。 Hope this helps !! 希望这可以帮助 !!

The problem is that mock endpoints just intercept the message before delegating to the actual endpoint. 问题在于,模拟端点只是在委派给实际端点之前截取了消息。 Quoted from the docs : 文档引用:

Important: The endpoints are still in action. 重要:端点仍在起作用。 What happens differently is that a Mock endpoint is injected and receives the message first and then delegates the message to the target endpoint. 不同的是,将模拟端点插入并首先接收消息,然后将消息委托给目标端点。 You can view this as a kind of intercept and delegate or endpoint listener. 您可以将其视为一种拦截和委托或端点侦听器。

The solution to your problem is to tell certain endpoints (the ones that expect a consumer in your case) not to delegate to the actual endpoint. 您问题的解决方案是告诉某些终结点(在您的情况下期望消费者的终结点) 不要委派给实际终结点。 This can easily be done using @MockEndpointsAndSkip instead of @MockEndpoints : 使用@MockEndpointsAndSkip而不是@MockEndpoints可以轻松完成此操作:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyRouteTest.TestConfig.class }, loader = CamelSpringDelegatingTestContextLoader.class)
@MockEndpointsAndSkip("direct:out") // <-- turns unit test from red to green ;)
public class MyRouteTest {
// ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM