2012-02-28 144 views
1

我试图找出在使用弹簧测试支持的集成测试中嘲讽端点的“正确”方法。用弹簧配置驼峰嘲弄骆驼端点上下文

该代码正在工作,但我想知道这是否是正确的方式。我已经看过骆驼测试测试工具包,它的编号为adviceWith,但是当spring负责在测试中加载camelContex时没有用,对吧?

这是我有:

服务:

@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://[email protected]", 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://[email protected]", fakeEndpoint); 

我在某处读到,使用@EndpointInject(uri = "mock:ftp")会创建一个模拟终端,其中包含默认FtpEndpoint,但如果我将其保留,则测试将失败,因为它使用默认值。

另一个奇怪的事情是,如果我使用“FTP *”的代替“的ftp:// FOO @本地”在嘲笑URI测试失败,以及,这使我相信,这是不是正确的做法。

任何帮助,非常感谢!

回答

3

我认为David Valeri正致力于改进骆驼测试春,以便能够使用纯粹的春季测试套件做更多的骆驼东西。有JIRA票,所以请留意未来的改进。

起初,尽管你可以使用Spring属性占位符,来代替端点的URI,因此在运行测试时,可以替代实际的FTP端点,以模拟终端等

看到这个常见问题关于Spring XML限制 http://camel.apache.org/how-do-i-use-spring-property-placeholder-with-camel-xml.html

第6章中的骆驼在行动的书,还介绍了如何使用Spring属性占位符测试,并有包含实际的端点的URI一个test.properties和production.properties文件。

在您的测试方法中,您可以使用Camel advice-with API在运行测试之前更改路由和内容。查看详情这里:http://camel.apache.org/advicewith.html

+0

非常感谢!在这种特殊情况下,我希望端点uri是动态的(由ExportProperties决定),所以我想这意味着我现在必须手动添加端点模拟?此外,我没有使用任何路线(我认为),所以这意味着adwiceWith API是没用的吗?或者直接调用ProducerTemplate创建动态路由? – ebaxt 2012-02-29 09:44:16

+0

无建议 - 仅适用于路线。 ProducerTemplate将直接从注册表中查找端点。您可以使用自定义EndpointStrategy并使用addRegisterEndpointCallback将其注册到CamelContext,然后您可以影响正在注册哪个uri端点。然而,它有点自己做,还有骆驼如何做一些拦截发送到端点,并发送到嘲笑。 – 2012-02-29 10:04:26

+0

再次感谢!最后一个问题:)为什么我不能像这样手动“替换”FtpEndpoint:'camelContext.addEndpoint(“ftp”,fakeEndpoint);'?我认为协议决定将它发送到哪个端点的实现?如果是这样,主持人应该不会有什么关系?为了得到它的工作,我必须明确地用完整的uri代替端点,如:'camelContext.addEndpoint(“ftp:// foo @ localhost”,fakeEndpoint);'。 – ebaxt 2012-02-29 10:13:46

0

我写了a small article使用mockAllEndpoints骆驼(> 2.7)的新功能。 你也可以找到官方文档here

+0

谢谢,但我不认为这适用,因为我没有定义任何路线,而是直接使用生产者模板? – ebaxt 2012-02-29 08:45:24

0

尝试扩展测试类

@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相关的问题