Note that there are some explanatory texts on larger screens.

plurals
  1. POMocking Camel endpoints with spring-configured camelContext
    text
    copied!<p>I'm trying to figure out the "correct" way of mocking out an Endpoint in an integration test that uses spring test support.</p> <p>The code is working, but I'm wondering if this is the correct way of doing it. I've looked at the camel-test Test Kit and it's <a href="http://camel.apache.org/advicewith.html" rel="nofollow">adviceWith</a>, but that is not useful when spring is responsible for loading the camelContex in the test, right?</p> <p>This is what I have:</p> <p><strong>The service:</strong></p> <pre><code>@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()); } } </code></pre> <p><strong>The integration test:</strong></p> <pre><code>@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(); } } </code></pre> <p>Now, this works, but I'm wondering if this is sort of a hack:</p> <pre><code>camelContext.addEndpoint("ftp://foo@localhost", fakeEndpoint); </code></pre> <p>I read somewhere that using <code>@EndpointInject(uri = "mock:ftp")</code> would create a mocked endpoint with higher present that the default FtpEndpoint, but if I leave this out the test fails because it's using the default.</p> <p>Another strange thing is that if i use <strong>"ftp*"</strong> instead of <strong>"ftp://foo@localhost"</strong> in the mocks uri the test fails as well, which has led me to believe that this is not the correct way of doing it.</p> <p>Any help is greatly appreciated! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload