Spring 测试apachecamel路由
我使用ApacheCamel 2.15.3构建了一个应用程序。我正在使用SpringXML连接路由以进行依赖注入。我很难理解如何为我的路线编写自动测试。例如,我可能有以下路线:Spring 测试apachecamel路由,spring,mocking,apache-camel,automated-tests,Spring,Mocking,Apache Camel,Automated Tests,我使用ApacheCamel 2.15.3构建了一个应用程序。我正在使用SpringXML连接路由以进行依赖注入。我很难理解如何为我的路线编写自动测试。例如,我可能有以下路线: <onException id="Exception"> <exception>java.lang.Exception</exception> <handled> <constant>true<
<onException id="Exception">
<exception>java.lang.Exception</exception>
<handled>
<constant>true</constant>
</handled>
<to uri="direct:fear"/>
</onException>
<route id="happyStory">
<from uri="direct:inTheBeginning"/>
<to uri="bean:enchantedKingdom?method=warn" />
<to uri="bean:fluffykins" />
</route>
<route id="scaryStory">
<from uri="direct:fear"/>
<onException>
<exception>java.lang.Exception</exception>
<handled>
<constant>true</constant>
</handled>
</onException>
<to uri="bean:monster"/>
<choice>
<when>
<simple>${header.succesfullywarned}</simple>
<to uri="bean:enchantedKingdom?method=hide"/>
</when>
<otherwise>
<to uri="bean:enchantedKingdom?method=panic" />
</otherwise>
</choice>
</route>
}
我已经阅读了《骆驼行动》第一版中关于测试的章节,并查看了手册中的内容,但我不明白如何将其应用于我的情况。在上面的测试中,除了我有带多个方法的bean之外,其他一切都可以工作,所以我有一个包含?method=methodname的uri,出于某种原因,这使得它无法工作。我没有得到错误或错误,但是没有使用mock,而是调用实际的bean。
不可能做我不想做的事吗?我可以以任何方式更改测试设置,但路由和bean是在SpringXML文件中定义的
我教过如何模拟bean本身,而不是端点,但我能想到的唯一方法是创建一个imposter-beans.xml文件,其中定义了所有bean,指向扩展路由中使用的每个类的stubbclass。但这感觉像是一种精心设计的错误方法。您可以编写代码,在mock收到消息时执行什么操作。第6.2.6节的书中介绍了这一点,您可以使用whenAnyExchangeReceived或WhenNexChangeReceived等方法,在这些内联处理器中,您可以设置头或抛出异常等。例如,请参见清单6.9。是的,我想这是我不希望看到的。以前我试过,但没能成功。现在我又尝试了一次,并且做得更进一步了,但是我不能让它与一个有多个方法作为端点的bean一起工作。我已编辑我的帖子以显示此设置。这是不可能的还是我遗漏了什么?
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration({"/META-INF/spring/route-
stories.xml","/META-INF/spring/beans.xml"})
@MockEndpointsAndSkip("(bean:fluffykins|bean:monster|bean:enchantedKingdom?method=warn|bean:enchantedKingdom?method=hide|bean:enchantedKingdom?method=panic)")
public class StoryHappyRouteTest extends CamelSpringTestSupport {
private String url = "direct:inTheBeginning";
@Autowired
private ApplicationContext applicationContext;
@Override
protected AbstractApplicationContext createApplicationContext() {
return (AbstractApplicationContext)applicationContext;
}
@Test
public void test(){
MockEndpoint warn = getMockEndpoint("mock:bean:enchantedKingdom?method=warn");
MockEndpoint fluffy = getMockEndpoint("mock:bean:fluffykins");
MockEndpoint monster = getMockEndpoint("mock:bean:monster");
MockEndpoint hide = getMockEndpoint("mock:bean:enchantedKingdom?method=hide");
MockEndpoint panic = getMockEndpoint("mock:bean:enchantedKingdom?method=panic");
fluffy.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
System.out.println("Bunny!");
throw new NullPointerException();
}
});
template.sendBody(url,"");
warn.assertExchangeReceived(0);
fluffy.assertExchangeReceived(0);
monster.assertExchangeReceived(0);
panic.assertExchangeReceived(0);
}