2012-02-28 93 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相關的問題