我正在尝试编写一个SpringBootTest来测试骆驼路线。我的路线如下所示:

 restConfiguration().producerComponent("http4")
            .host("http://127.0.0.1);

    from("rabbitmq:foo")
            .to(rest:post")
            .log("Hello!: ${body}");


这是我的测试:

@RunWith(CamelSpringRunner.class)
@MockEndpoints
@UseAdviceWith
@SpringBootTest
public class SimpleCamelRouteTest extends CamelTestSupport {

@EndpointInject(uri = "mock:rest")
private MockEndpoint mockEndpoint;

@Autowired
CamelContext context;

@Autowired
ProducerTemplate template;

@Before
public void setUp() throws Exception {

    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
       @Override
        public void configure() throws Exception {
           interceptSendToEndpoint("rabbitmq:foo")
                   .skipSendToOriginalEndpoint()
                   .to("mock:foo");
           interceptSendToEndpoint("http://*")
                   .skipSendToOriginalEndpoint()
                   .to("mock:rest");
        }
    });
    context.start();

}


@Test
public void test() throws InterruptedException {
    String body = "Camel";
    mockEndpoint.expectedMessageCount(1);

    template.sendBody("mock:foo", body);

    mockEndpoint.assertIsSatisfied();
}

}


看起来它正在尝试在启动时连接到RabbitMq的实际运行实例:(

18-08-2019 13:20:07.729 [Camel (camel-1) thread #3 - RabbitMQConsumer] INFO  o.a.c.c.rabbitmq.RabbitMQConsumer.call - Connection failed, will retry in 5000ms


java.net.ConnectException:连接被拒绝(连接被拒绝)

有人可以给我一些建议,告诉我如何告诉我的SpringBootTest不要寻找正在运行的代理,并尊重我设置的模拟程序(假设模拟程序设置正确)。

谢谢

最佳答案

您正在尝试使用from拦截使用者(interceptSendToEndpoint)。这不可能。为此,您需要replaceFromWith

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
   @Override
    public void configure() throws Exception {
       replaceFromWith("direct:triggerFoo");
       //...
    }
});


然后触发这样的路由:

template.sendBody("direct:triggerFoo", body);




另外,您正在拦截http4生产者,但是从您的路线看来,您可能想要拦截rest*

10-07 19:36
查看更多