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

时间:2013-01-15 11:00:34

标签: java cxf apache-camel

我开始使用Camel,我在编写测试时遇到了一些问题。我的用例与cfx proxy example完全相同。除了我不需要“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);
}
}

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>

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

1 个答案:

答案 0 :(得分:4)

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

我们在camel-context中定义属性

<propertyPlaceholder id="properties"
            location="classpath:META-INF/uri.properties" xmlns="http://camel.apache.org/schema/spring" />

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

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

<route>
  <from uri="{{cxf.bean.cxfEndpoint}}"/>
</route>

主要的uri.properties将是

cxf.bean.cxfEndpoint=cxf:bean:cxfEndpoint?dataFormat=MESSAGE

测试uri.properties将是

cxf.bean.cxfEndpoint=direct:start

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