Unit testing 骆驼测试-如何跳过处理器内ConsumerTemplate中使用的端点
我实现了一个使用轮询消费者的服务。该模式与此模式非常相似(请参阅“基于计时器的轮询消费者”) 这是我的变体:Unit testing 骆驼测试-如何跳过处理器内ConsumerTemplate中使用的端点,unit-testing,apache-camel,Unit Testing,Apache Camel,我实现了一个使用轮询消费者的服务。该模式与此模式非常相似(请参阅“基于计时器的轮询消费者”) 这是我的变体: @Override public void configure() throws Exception { from("quartz2://" + SCHEDULE + "?cron=" + checkNotNull(cron)).routeId(IN_ROUTE) .process(fetchMessages) ; } 获取消息处理器: @Ove
@Override
public void configure() throws Exception {
from("quartz2://" + SCHEDULE + "?cron=" + checkNotNull(cron)).routeId(IN_ROUTE)
.process(fetchMessages)
;
}
获取消息处理器:
@Override
public void process(Exchange exchange) throws Exception {
while (true) {
exchange = consumerTemplate.receive(MainRoute.ACTIVE_MQ + ":queue:" + "source", TIMEOUT);
//no more messages within timeout
if (exchange == null){
break;
}
producerTemplate.send(MidRoute.MID_PLUG, exchange);
}
}
我的问题是:如何跳过测试中的activemq端点
到目前为止,我试图通过camels测试支持跳过它:
@Override
public String isMockEndpointsAndSkip() {
return "(" + MainRoute.ACTIVE_MQ + ".*)|" + MidRoute.MID_PLUG;
}
和截距:
@Before
public void prepare() throws Exception {
context.getRouteDefinition(InRoute.IN_ROUTE).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith(in);
interceptSendToEndpoint(MainRoute.ACTIVE_MQ + ":queue:" + "source")
.skipSendToOriginalEndpoint()
.to(amqMockSource);
}
});
}
两者都没有用。仍然调用ActiveMq端点,导致连接被拒绝异常
那么如何跳过bean中使用的端点呢?您可以使用Camel中的weave机制来实现这一点
.routeId(“)
为您的路线指定一个id,然后为您的.process(“”)指定一个id。因此,请编写.process(“fetchMessages”).id(“myId”)
@Override
public boolean isUseAdviceWith() {
return true;
}
@Before
public void setUp() throws Exception {
super.setUp();
context.getRouteDefinition("YourRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("myId").replace().setBody().constant("Some response"));
}
});
}
基本上,我们是说我们希望使用AdviceWith,然后我们在具有给定routeId的路由中查找具有给定id的给定方法,并将其替换为某个响应体。这是您希望从activemq收到的响应。您可以使用Camel中的weave机制来实现这一点
.routeId(“)
为您的路线指定一个id,然后为您的.process(“”)指定一个id。因此,请编写.process(“fetchMessages”).id(“myId”)
@Override
public boolean isUseAdviceWith() {
return true;
}
@Before
public void setUp() throws Exception {
super.setUp();
context.getRouteDefinition("YourRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("myId").replace().setBody().constant("Some response"));
}
});
}
基本上,我们是说我们希望使用AdviceWith,然后我们在带有给定routeId的路由中查找具有给定id的给定方法,并用一些响应体替换它。这是您希望从activemq收到的响应。谢谢,我希望有一种方法可以使用处理器测试路由。没有处理器,路由几乎什么也做不到。在处理器中,您主要是从某个activemq队列接收消息?或者,如果您想进行集成测试,您可以在单元测试中创建一个路由,并允许它将消息放在本地activemq实例上。谢谢-集成测试是我做的第一个测试,而且非常成功运行良好。谢谢,我希望有办法用处理器测试路由。没有处理器,路由几乎什么也做不到。在处理器中,您主要是从某个activemq队列接收消息?或者,如果您想进行集成测试,您可以在单元测试中创建一个路由,并允许它将消息放在本地activemq实例上。谢谢-集成测试是我做的第一个测试,而且非常成功运行良好。