我試圖找出在使用彈簧測試支持的集成測試中嘲諷端點的「正確」方法。用彈簧配置駝峯嘲弄駱駝端點上下文
該代碼正在工作,但我想知道這是否是正確的方式。我已經看過駱駝測試測試工具包,它的編號爲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