骆驼处理器 unit/integration 测试
Camel processor unit/integration testing
我可能完全错过了一些东西,但我无法按照自己的意愿测试我的路线。
我有以下 bean :
@Component("fileProcessor")
public class FileProcessor {
public boolean valid(@Header("customObject) CustomObject customObject,Exchange exchange) throws IOException{
return false;
}
我有一个像这样调用我的 bean 的路由:
from("direct:validationFile").routeId("validationFile").validate().method("fileProcessor","valid")
// Other stuff
.end();
这是我的单元测试,基于我发现的一个例子:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class})
@ContextConfiguration(locations = { "classpath:/tu-dao-beans.xml" })
public class FileProcessorTest extends CamelTestSupport {
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri = "direct:start")
protected ProducerTemplate template;
@Override
public boolean isDumpRouteCoverage() {
return true;
}
@Test
public void testSendMatchingMessage() throws Exception {
String expectedBody = "<matched/>";
resultEndpoint.expectedBodiesReceived(expectedBody);
template.sendBodyAndHeader(expectedBody, "foo", "bar");
resultEndpoint.assertIsSatisfied();
}
@Test
public void testSendNotMatchingMessage() throws Exception {
resultEndpoint.expectedMessageCount(0);
template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");
resultEndpoint.assertIsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
// from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
from("direct:start").routeId("validationFile").validate().method("fileProcessor","valid").to("mock:result");
}
};
}
}
测试失败,因为未找到 fileProcessor
,但我很确定我的 spring context
已正确加载,我正在为我的 [= 使用相同的 beans.xml
文件18=] 测试和我的 DAO 组件都很好...我错过了什么?
编辑:
感谢 Jérémis B 的回答,我轻松地解决了我的问题。万一有人像我一样绊倒了,这里是我添加的代码:
@Autowired
private FileProcessor fileProcessor;
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
registry.bind("fileProcessor", fileProcessor);
return registry;
}
您可以看到 official documentation 的 "How to" 测试 Spring。
在您的示例中,您创建了一个 Spring 上下文,但使用了 CamelTestSupport
:此 class 创建了一个不知道 Spring 上下文的 CamelContext。此上下文未看到 bean "fileProcessor"。
有很多方法可以进行这种测试。使用您已有的代码,最简单的方法可能是:
- 在你的测试中注入 fileProcessor class,使用 @Autowire
- 覆盖
createRegistry
并将文件处理器添加到注册表
您也可以覆盖 CamelSpringTestSupport
并实施 createApplicationContext
。另一种方法是将路由定义保留在 Spring Bean 中(通过 xml 或 RouteBuilder),并在测试中注入 MockEndpoint
s 或 ProducerTemplate
.
我可能完全错过了一些东西,但我无法按照自己的意愿测试我的路线。
我有以下 bean :
@Component("fileProcessor")
public class FileProcessor {
public boolean valid(@Header("customObject) CustomObject customObject,Exchange exchange) throws IOException{
return false;
}
我有一个像这样调用我的 bean 的路由:
from("direct:validationFile").routeId("validationFile").validate().method("fileProcessor","valid")
// Other stuff
.end();
这是我的单元测试,基于我发现的一个例子:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class})
@ContextConfiguration(locations = { "classpath:/tu-dao-beans.xml" })
public class FileProcessorTest extends CamelTestSupport {
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri = "direct:start")
protected ProducerTemplate template;
@Override
public boolean isDumpRouteCoverage() {
return true;
}
@Test
public void testSendMatchingMessage() throws Exception {
String expectedBody = "<matched/>";
resultEndpoint.expectedBodiesReceived(expectedBody);
template.sendBodyAndHeader(expectedBody, "foo", "bar");
resultEndpoint.assertIsSatisfied();
}
@Test
public void testSendNotMatchingMessage() throws Exception {
resultEndpoint.expectedMessageCount(0);
template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");
resultEndpoint.assertIsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
// from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
from("direct:start").routeId("validationFile").validate().method("fileProcessor","valid").to("mock:result");
}
};
}
}
测试失败,因为未找到 fileProcessor
,但我很确定我的 spring context
已正确加载,我正在为我的 [= 使用相同的 beans.xml
文件18=] 测试和我的 DAO 组件都很好...我错过了什么?
编辑: 感谢 Jérémis B 的回答,我轻松地解决了我的问题。万一有人像我一样绊倒了,这里是我添加的代码:
@Autowired
private FileProcessor fileProcessor;
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
registry.bind("fileProcessor", fileProcessor);
return registry;
}
您可以看到 official documentation 的 "How to" 测试 Spring。
在您的示例中,您创建了一个 Spring 上下文,但使用了 CamelTestSupport
:此 class 创建了一个不知道 Spring 上下文的 CamelContext。此上下文未看到 bean "fileProcessor"。
有很多方法可以进行这种测试。使用您已有的代码,最简单的方法可能是:
- 在你的测试中注入 fileProcessor class,使用 @Autowire
- 覆盖
createRegistry
并将文件处理器添加到注册表
您也可以覆盖 CamelSpringTestSupport
并实施 createApplicationContext
。另一种方法是将路由定义保留在 Spring Bean 中(通过 xml 或 RouteBuilder),并在测试中注入 MockEndpoint
s 或 ProducerTemplate
.