使用弹簧配置的camelContext模拟Camel端点



我正试图找出在使用spring测试支持的集成测试中模拟端点的"正确"方法。

代码正在运行,但我想知道这是否是正确的方法。我已经查看了camelTest Kit,它是adviceWith,但当spring负责在测试中加载camelContext时,这是没有用的,对吧?

这就是我所拥有的:

服务:

@Service
public class FtpOutboundFileStrategy implements OutboundFileExportStrategy {
    private final String FTP_PATTERN= "{0}://{1}@{2}";
    private final ProducerTemplate producerTemplate;
    @Autowired
    public FtpOutboundPriceFileStrategy(ProducerTemplate producerTemplate) {
        this.producerTemplate = producerTemplate;
    }
    @Override
    public void doExport(OutboundFile file, ExportProperties exportProperties) {
        this.producerTemplate.sendBodyAndHeader(createFtpUri(exportProperties),
                file.getFileContent(), Exchange.FILE_NAME, file.getFileName());
    }
}

集成测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:testDB.xml", "classpath:applicationContext.xml"})
public class FtpOutboundFileStrategyIT {
    @EndpointInject(uri = "mock:ftp")
    protected MockEndpoint fakeEndpoint;
    @Autowired
    FtpOutboundFileStrategy ftpOutboundPriceFileStrategy;
    @Autowired
    protected CamelContext camelContext;
    @DirtiesContext
    @Test
    public void directsToFtpEndpoint() throws Exception {
        camelContext.addEndpoint("ftp://foo@localhost", fakeEndpoint);
        fakeEndpoint.expectedBodyReceived().equals("This is the file");
        ftpOutboundPriceFileStrategy.doExport(new OutboundFile("This is the file"),
                new ExportProperties("foo", "localhost"));
        fakeEndpoint.assertIsSatisfied();
    }
}

现在,这是有效的,但我想知道这是否是一种黑客攻击:

camelContext.addEndpoint("ftp://foo@localhost", fakeEndpoint);

我在某个地方读到,使用@EndpointInject(uri = "mock:ftp")会创建一个比默认FtpEndpoint更高的模拟端点,但如果我忽略了这一点,测试就会失败,因为它使用了默认值。

另一件奇怪的事情是,如果我使用"ftp*"而不是ftp://foo@localhost"在mocks uri中,测试也失败了,这让我相信这不是正确的方法。

非常感谢您的帮助!

我认为David Valeri正在改进骆驼测试弹簧,以便能够用纯弹簧测试套件做更多的骆驼测试。有一张JIRA的票,所以请留意未来的改进。

起初,尽管您可以使用Spring属性占位符来替换端点uri,所以在运行测试时,您可以用模拟端点等替换实际的ftp端点。

请参阅有关Spring XML限制的常见问题解答http://camel.apache.org/how-do-i-use-spring-property-placeholder-with-camel-xml.html

Camel in Action一书的第6章还介绍了如何使用Spring属性占位符进行测试,并有一个包含实际端点uri的test.properties和production.properties文件。

在您的测试方法中,您可以使用Camel advice-withneneneba API在运行测试之前更改路由等等。请在此处查看详细信息:http://camel.apache.org/advicewith.html

我写了一篇关于使用Camel(>2.7)的mockAllEndpoints新功能的小文章。你也可以在这里找到官方文件。

尝试使用扩展测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyConfigurationClass.class)
//this could be an xml file as well with locations attribute
public class CamelRoutesTest extends AbstractJUnit4SpringContextTests{

我也有类似的问题。这个修复了camelContext和applicationContext相关的问题

最新更新