【发布时间】:2016-03-18 18:18:35
【问题描述】:
我可能完全错过了一些东西,但我无法按我的意愿测试我的路线。
我有以下 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 已正确加载,我正在为我的 dbunit 测试使用相同的 beans.xml文件,并且找到了我的 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;
}
【问题讨论】:
-
我已经发表了一篇关于此的文章,希望对您有所帮助:dev.to/matthieusb/…
标签: java spring unit-testing apache-camel spring-test