Java Camel处理器单元/集成测试
我可能完全错过了一些东西,但我无法按照我的意愿测试我的路线 我得到了以下bean:Java Camel处理器单元/集成测试,java,spring,unit-testing,apache-camel,spring-test,Java,Spring,Unit Testing,Apache Camel,Spring Test,我可能完全错过了一些东西,但我无法按照我的意愿测试我的路线 我得到了以下bean: @Component("fileProcessor") public class FileProcessor { public boolean valid(@Header("customObject) CustomObject customObject,Exchange exchange) throws IOException{ return false; } 我有一条路线像这样调用我的be
@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");
}
};
}
}
您可以看到有关如何使用Spring进行测试的详细信息
在您的示例中,您创建了一个Spring上下文,但使用了CamelTestSupport
:这个类创建了一个CamelContext,它不知道Spring上下文。此上下文看不到bean“fileProcessor”
有很多方法可以做这种测试。使用您已有的代码,最简单的方法可能是:
- 使用@Autowire在测试类中注入文件处理器
- 覆盖
并将文件处理器添加到注册表中createRegistry
您也可以覆盖
CamelSpringTestSupport
并实现createApplicationContext
。另一种方法是将路由定义保存在Springbean中(通过xml或RouteBuilder),并将其插入测试MockEndpoint
s或ProducerTemplate
,我已经发表了一篇关于这方面的文章,希望能有所帮助:
@Autowired
private FileProcessor fileProcessor;
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
registry.bind("fileProcessor", fileProcessor);
return registry;
}