如何解决如何在Apache Camel中对生产路线进行单元测试?
| 假设我在单独的RouteBuilder类中创建了我的路由。看起来像: 从JMS队列中获取消息 做一些转换,验证等 根据验证结果转发到特定的JMS队列,然后将某些内容保存在DB中 我想在没有JMS代理和DB的情况下对该路径进行单元测试。我知道我可以模拟我的处理器实现,但这还不够。我不想更改此路由(假设我在jar文件中获得了该类)。据我从《骆驼在行动》(6.2.6节)中了解到的,要能够使用端点模拟和其他内容,我需要更改我的路线端点定义(在本书的示例中,这是对“ mina”的更改:tcp:// miranda \“到\” mock:miranda \“等)。 是否可以在不更改路由定义的情况下完全隔离地测试流? 如果我将RouteBuilder作为一个单独的类获得,是否必须以某种方式“复制”路由定义并手动进行更改?它不是在测试错误的东西吗? 我对Camel还是很陌生,对我而言,能够在开发路线时进行隔离的单元测试真的很酷。只是为了能够改变某些东西,进行小测试,观察结果等等。解决方法
假设RouteBuilder类已经对端点进行了硬编码,那么它的测试就更难了。但是,如果RouteBuilder将属性占位符用于端点uri,则通常可以将一组不同的端点uri用于单元测试。正如骆驼书第6章中所述。
如果它们是硬编码的,则可以在单元测试中使用带有功能的建议,如下所示:http://camel.apache.org/advicewith.html
在Camel 2.7中,我们使操纵路线变得更加容易,因此您可以删除零件,更换零件等。这就是链接所谈论的编织内容。
例如,为了模拟向数据库端点发送消息,您可以使用上面的消息,并用将其替换为,然后再将其发送到模拟对象。
在以前的版本中,您可以使用interceptSendToEndpoint技巧,在Camel书中也有介绍(第6.3.3节)
哦,您也可以使用模拟组件替换组件,如第169页所示。从Camel 2.8起,模拟组件将不再抱怨它不知道的uri参数。这意味着在每个组件级别用模拟替换组件要容易得多。
,我有
<bean id=\"properties\" class=\"org.apache.camel.component.properties.PropertiesComponent\">
<property name=\"location\" value=\"classpath:shop.properties\"/>
</bean>
<route>
<from uri=\"direct://stock\"/>
<to uri=\"{{stock.out}}\"/>
</route>
在我的spring文件中,然后在测试类路径上的shop.properties中,我有一个stock.out = xxxx,它在运行时被替换,因此我可以使用不同的路由,一个用于运行,另一个用于测试
在多个环境中的6.1.6单元测试中有一个更好的示例
,尽管您可以根据克劳斯·易卜生的使用拦截和建议来交换端点
答案,我认为最好允许您的路线接受ѭ1far
实例,这样您的测试就不会与生产端点URI耦合。
例如,假设您有一个RouteBuilder
,看起来像
public class MyRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from(\"http://someapi/someresource\")
.process(exchange -> {
// Do stuff with exchange
})
.to(\"activemq:somequeue\");
}
}
您可以像这样注入端点:
public class MyRoute extends RouteBuilder {
private Endpoint in;
private Endpoint out;
// This is the constructor your production code can call
public MyRoute(CamelContext context) {
this.in = context.getEndpoint(\"http://someapi/someresource\");
this.out = context.getEndpoint(\"activemq:somequeue\");
}
// This is the constructor your test can call,although it would be fine
// to use in production too
public MyRoute(Endpoint in,Endpoint out) {
this.in = in;
this.out = out;
}
@Override
public void configure() throws Exception {
from(this.in)
.process(exchange -> {
// Do stuff with exchange
})
.to(this.out);
}
}
然后可以像这样测试:
public class MyRouteTest {
private Endpoint in;
private MockEndpoint out;
private ProducerTemplate producer;
@Before
public void setup() {
CamelContext context = new DefaultCamelContext();
this.in = context.getEndpoint(\"direct:in\");
this.out = context.getEndpoint(\"mock:direct:out\",MockEndpoint.class);
this.producer = context.createProducerTemplate();
this.producer.setDefaultEndpoint(this.in);
RouteBuilder myRoute = new MyRoute(this.in,this.out);
context.addRoutes(myRoute);
context.start();
}
@Test
public void test() throws Exception {
this.producer.sendBody(\"Hello,world!\");
this.out.expectedMessageCount(1);
this.out.assertIsSatisfied();
}
}
这具有以下优点:
您的测试非常简单易懂,甚至不需要扩展CamelTestSupport
或其他帮助程序类
CamelContext
是手工创建的,因此可以确保仅创建被测路线
测试不关心生产路线URI
您仍然可以方便地将端点URI硬编码到路由类中
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。