在我们的项目中,我们不会创建模拟FTP服务器来测试路由,而是使用可以被本地开发和单元测试中的
file
Camel组件替换的属性。
您的代码应该如下所示:
public class MyRoute extends RouteBuilder
{
@Override
public void configure()
{
onException(EdiOrderParsingException.class)
.handled(true)
.to("{{myroute.error}}");
from("{{myroute.input.endpoint}}")
.bean(new OrderEdiTocXml())
.convertBodyTo(String.class)
.convertBodyTo(Document.class)
.choice()
.when(xpath("/cXML/Response/Status/@text='OK'"))
.to("{{myroute.valid.endpoint}}}")
.otherwise()
.to("{{myroute.invalid.endpoint}}");
}
}
而在本地和系统测试中,我们使用在属性文件中声明的文件端点:
myroute.input.endpoint=file:/home/user/myproject/input
myroute.valid.endpoint=file:/home/user/myproject/valid
myroute.invalid.endpoint=file:/home/user/myproject/invalid
myroute.error=file:/home/user/myproject/error
或者在JUnit CamelTestSupport中,您可以使用useOverridePropertiesWithPropertiesComponent
方法来设置要覆盖的属性。
作为替代方案,您也可以使用“直接”路由,但是您可能会错过一些可以通过单元测试进行测试的文件选项。
我们只通过设置如下属性来测试与真实系统的FTP连接:
myroute.input.endpoint=ftp://hostname/input
myroute.valid.endpoint=ftp://hostname/valid
myroute.invalid.endpoint=ftp://hostname/invalid
myroute.error=ftp://hostname/error
通过这个,你也可以为不同的配置设置不同的环境,例如生产服务器和集成测试环境。
生产环境属性示例:
myroute.input.endpoint=ftp://hostname-prod/input
myroute.valid.endpoint=ftp://hostname-prod/valid
myroute.invalid.endpoint=ftp://hostname-prod/invalid
myroute.error=ftp://hostname-prod/error
在我看来,使用文件端点来简化JUnit代码并仅测试路由而不是连接是完全可以接受的。
测试连接更像是集成测试,应该在连接到真实外部系统的真实服务器上执行(在您的情况下是FTP服务器,但也可以是其他端点/系统)。
通过使用属性,您还可以针对每个环境配置不同的URL(例如:我们有3个测试环境和一个生产环境,所有这些环境都具有不同的端点)。