我试图找出在使用弹簧测试支持的集成测试中嘲讽端点的“正确”方法。用弹簧配置驼峰嘲弄骆驼端点上下文
该代码正在工作,但我想知道这是否是正确的方式。我已经看过骆驼测试测试工具包,它的编号为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测试失败,以及,这使我相信,这是不是正确的做法。
任何帮助,非常感谢!
非常感谢!在这种特殊情况下,我希望端点uri是动态的(由ExportProperties决定),所以我想这意味着我现在必须手动添加端点模拟?此外,我没有使用任何路线(我认为),所以这意味着adwiceWith API是没用的吗?或者直接调用ProducerTemplate创建动态路由? – ebaxt 2012-02-29 09:44:16
无建议 - 仅适用于路线。 ProducerTemplate将直接从注册表中查找端点。您可以使用自定义EndpointStrategy并使用addRegisterEndpointCallback将其注册到CamelContext,然后您可以影响正在注册哪个uri端点。然而,它有点自己做,还有骆驼如何做一些拦截发送到端点,并发送到嘲笑。 – 2012-02-29 10:04:26
再次感谢!最后一个问题:)为什么我不能像这样手动“替换”FtpEndpoint:'camelContext.addEndpoint(“ftp”,fakeEndpoint);'?我认为协议决定将它发送到哪个端点的实现?如果是这样,主持人应该不会有什么关系?为了得到它的工作,我必须明确地用完整的uri代替端点,如:'camelContext.addEndpoint(“ftp:// foo @ localhost”,fakeEndpoint);'。 – ebaxt 2012-02-29 10:13:46