Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用spring配置的camelContext模拟骆驼端点_Java_Spring_Junit_Integration Testing_Apache Camel - Fatal编程技术网

Java 使用spring配置的camelContext模拟骆驼端点

Java 使用spring配置的camelContext模拟骆驼端点,java,spring,junit,integration-testing,apache-camel,Java,Spring,Junit,Integration Testing,Apache Camel,我试图找出在使用spring测试支持的集成测试中模拟端点的“正确”方法 代码正在运行,但我想知道这是否是正确的方法。我已经看过了camel测试工具包,它非常有用,但是当spring负责在测试中加载camelContex时,这是没有用的,对吗 这就是我所拥有的: 服务: @Service public class FtpOutboundFileStrategy implements OutboundFileExportStrategy { private final String FTP_

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

代码正在运行,但我想知道这是否是正确的方法。我已经看过了camel测试工具包,它非常有用,但是当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://foo@localhost", fakeEndpoint);

        fakeEndpoint.expectedBodyReceived().equals("This is the file");
        ftpOutboundPriceFileStrategy.doExport(new OutboundFile("This is the file"),
                new ExportProperties("foo", "localhost"));
        fakeEndpoint.assertIsSatisfied();
    }
}
集成测试:

@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中,测试也失败了,这让我相信这不是正确的方法

非常感谢您的帮助

我写了一篇关于使用Camel(>2.7)的新特性的文章。
您还可以找到官方文档。

我认为David Valeri正在改进camel test spring,以便能够使用pure spring测试套件进行更多的camel测试。有一张JIRA的票,所以请留意未来的改进

首先,虽然您可以使用Spring属性占位符来替换端点URI,但是在运行测试时,您可以使用模拟端点等替换实际的ftp端点

请参阅此关于SpringXML限制的常见问题解答

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


在您的测试方法中,您可以在运行测试之前使用带有API的Camel通知来更改路由等等。请参阅此处的详细信息:

尝试使用扩展测试类

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

谢谢,但我认为这不适用,因为我没有定义任何路由,而是直接使用producer模板?非常感谢!在这种特定情况下,我希望端点uri是动态的(由ExportProperties决定),所以我想这意味着我现在必须手动添加端点模拟?而且我没有使用任何路由(我想),所以这意味着AdwiceWithAPI是无用的,对吗?或者直接调用ProducerTemplate是否会创建动态路由?没有仅适用于路由的建议。ProducerTemplate将直接从注册表中查找端点。您可以使用自定义EndpointStrategy,并使用addRegisterEndpointCallback将其注册到CamelContext,然后您可以影响正在注册的uri端点。不过这有点像是自己动手做的,还有Camel是如何将一些截获发送到端点以及发送到mock的。再次感谢!最后一个问题:)为什么我不能像这样手动“替换”FtpEndpoint:
camelContext.addEndpoint(“ftp”,fakeEndpoint)?我认为协议决定了它被分派到的端点的哪个实现?如果是这样的话,主人是什么就不重要了?为了让它工作,我必须显式地将端点替换为完整uri,如:
camelContext.addEndpoint(“ftp://foo@本地主机”,fakeEndpoint)。如果旧的端点已经在使用中,您需要删除它们,CamelContext上有API可以删除端点,它支持通配符。例如,如果生产者模板已经查找了真正的ftp端点,然后您稍后替换了它,那么生产者仍然会使用真正的端点。这是关于鸡蛋的,先来。顺便说一句,我们在骆驼2.10中添加了一个新的存根组件: