我刚开始使用 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>
我做错了什么?我发现的所有示例和其他问题似乎都使用“direct:start”或更改生产路线。
最佳答案
我们成功使用的一种方法是为测试执行和主要代码设置不同的属性文件。
我们在 Camel 上下文中定义属性
<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
使用此配置,您将能够轻松地测试您的路线。
关于java - 如何使用模拟端点在 Camel 测试中启动路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14336274/