如何使用模拟端点在Camel测试中启动路由

Nic*_*rts 8 java cxf apache-camel

我开始使用Camel,我在编写测试时遇到了一些问题.我的用例与cfx代理示例完全相同.除了我不需要"RealWebservice".现在我正在尝试使用注释方法编写单元测试(不是示例中包含的集成测试):

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
@MockEndpointsAndSkip
public class RoutesTest {

@Autowired
CamelContext camelContext;

@EndpointInject(uri = "mock:cxf:bean:cxfEndpoint", context = "camelContext")
MockEndpoint cxfEndpoint;

@EndpointInject(uri = "mock:log:input", context = "camelContext")
MockEndpoint logInputEndpoint;

@EndpointInject(uri = "mock:http:realhostname:8211/service", context = "camelContext")
MockEndpoint realEndpoint;

@EndpointInject(uri = "mock:cxf:bean:cxfEndpoint")
ProducerTemplate producer;

@Test
public void testLeleuxMifidRoute() throws InterruptedException {
    String body = "<blah/>";

    cxfEndpoint.expectedBodiesReceived(body);
    logInputEndpoint.expectedBodiesReceived(body);
    realEndpoint.expectedBodiesReceived(body);

    producer.sendBody(body);

    MockEndpoint.assertIsSatisfied(camelContext);
}
}
Run Code Online (Sandbox Code Playgroud)

cxfEndpoint接收消息但其他端点不接收消息.

路径看起来像这样(当我运行它并使用SoapUI发送消息时,它很有效,显然我在这个例子中混淆了ips和beannames):

<endpoint id="callRealWebService" uri="http://realhostname:8211/service?throwExceptionOnFailure=true" /> 
<route>
  <from uri="cxf:bean:cxfEndpoint?dataFormat=MESSAGE"/>
  <to uri="log:input?showStreams=true"/>
  <to ref="callRealWebService"/>
  <to uri="log:output"/>
</route>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我发现的所有例子和其他问题似乎都使用"直接:开始"或改变生产路线.

MPa*_*esi 4

我们成功使用的一种方法是为测试执行和主代码使用不同的属性文件。

我们在骆驼上下文中定义属性

<propertyPlaceholder id="properties"
            location="classpath:META-INF/uri.properties" xmlns="http://camel.apache.org/schema/spring" />
Run Code Online (Sandbox Code Playgroud)

在该文件夹中,/src/main/resources/META-INF/我们有用于主代码的 uri.properties 文件,以及/src/test/resources/META-INF/用于测试执行的 uri.properties 文件。

您的路线必须使用属性占位符而不是真实的 uri 值重写,使用符号{{properties.name}}

<route>
  <from uri="{{cxf.bean.cxfEndpoint}}"/>
</route>
Run Code Online (Sandbox Code Playgroud)

主要的 uri.properties 将是

cxf.bean.cxfEndpoint=cxf:bean:cxfEndpoint?dataFormat=MESSAGE
Run Code Online (Sandbox Code Playgroud)

测试 uri.properties 将是

cxf.bean.cxfEndpoint=direct:start
Run Code Online (Sandbox Code Playgroud)

使用此配置,您将能够轻松测试您的路线。