单位测试Apache骆驼特定的路线(型号#setRoutefilter)
如何在我的单位测试中仅包含某些路线。例如,如何仅启用my-translation-route
。
public class TestRoute extends RouteBuilder {
@Override
public void configure() {
from("ftp://my-ftp-server:21/messages")
.routeId("my-inbound-route")
.to("direct:my-translation-route");
from("direct:my-translation-route")
.routeId("my-translation-route")
.bean(MyBean.class)
.to("direct:my-outbound-route");
from ("direct:my-outbound-route")
.routeId("my-translation-route")
.to("http://my-http-server:8080/messages");
}
}
我尝试使用型号#FilterRoutes,但这无效。所有路线均已加载。
class TestRouteTest extends CamelTestSupport {
@Override
protected RoutesBuilder createRouteBuilder() {
return new TestRoute();
}
@Override
public boolean isUseAdviceWith() {
return true;
}
@Test
void testIfItWorks() throws Exception {
context.setRouteFilterPattern("my-translation-route", null);
AdviceWith.adviceWith(context, "my-translation-route", a -> {
a.mockEndpointsAndSkip("direct:my-outbound-route");
});
context.start();
getMockEndpoint("mock:direct:my-outbound-route").expectedBodyReceived().expression(constant("Hahaha! 42"));
template.sendBodyAndHeaders("direct:my-translation-route", "42", null);
assertMockEndpointsSatisfied();
}
}
我将其与cameltestSupport#getRoutefilterIncludePattern
的覆盖效力一起工作,例如:
@Override
public String getRouteFilterIncludePattern() {
return "direct:my-translation-route";
}
但是,此测试中的所有测试都设置了。
How to include only certain routes in my unit test. For example, how do I enable only my-translation-route
.
public class TestRoute extends RouteBuilder {
@Override
public void configure() {
from("ftp://my-ftp-server:21/messages")
.routeId("my-inbound-route")
.to("direct:my-translation-route");
from("direct:my-translation-route")
.routeId("my-translation-route")
.bean(MyBean.class)
.to("direct:my-outbound-route");
from ("direct:my-outbound-route")
.routeId("my-translation-route")
.to("http://my-http-server:8080/messages");
}
}
I tried with Model#filterRoutes but this did not work. All routes were loaded.
class TestRouteTest extends CamelTestSupport {
@Override
protected RoutesBuilder createRouteBuilder() {
return new TestRoute();
}
@Override
public boolean isUseAdviceWith() {
return true;
}
@Test
void testIfItWorks() throws Exception {
context.setRouteFilterPattern("my-translation-route", null);
AdviceWith.adviceWith(context, "my-translation-route", a -> {
a.mockEndpointsAndSkip("direct:my-outbound-route");
});
context.start();
getMockEndpoint("mock:direct:my-outbound-route").expectedBodyReceived().expression(constant("Hahaha! 42"));
template.sendBodyAndHeaders("direct:my-translation-route", "42", null);
assertMockEndpointsSatisfied();
}
}
I got it working with the override of CamelTestSupport#getRouteFilterIncludePattern
, e.g.:
@Override
public String getRouteFilterIncludePattern() {
return "direct:my-translation-route";
}
But then this is set for all tests in this test class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能的(愚蠢的)解决方案:为您的路线设置有条件的自动启动,其值取决于(骆驼或JVM)属性,您可以在单元测试期间设置特定值:
在测试过程中有多种覆盖属性的方法。 See https://camel.apache.org/components/3.17.x /properties-component.html
Possible (stupid) solution : set a conditional auto startup for your routes, whose value depends on a (Camel or JVM) property that you can set with a particular value during the unit tests:
There are various ways to override properties during your tests. See https://camel.apache.org/components/3.17.x/properties-component.html