骆驼 RabbitMQ 到 REST 端点 SpringBootTest 失败
Camel RabbitMQ to REST Endpoint SpringBootTest failing
我正在尝试编写 SpringBootTest 来测试 Camel 路由。我的路线如下所示:
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 不要寻找 运行 代理并尊重我设置的模拟(假设模拟设置正确。)
谢谢
您正在尝试使用 interceptSendToEndpoint
拦截消费者 (from
)。这不可能。为此你需要 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*
。
我正在尝试编写 SpringBootTest 来测试 Camel 路由。我的路线如下所示:
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 不要寻找 运行 代理并尊重我设置的模拟(假设模拟设置正确。)
谢谢
您正在尝试使用 interceptSendToEndpoint
拦截消费者 (from
)。这不可能。为此你需要 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*
。