Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Camel处理器单元/集成测试_Java_Spring_Unit Testing_Apache Camel_Spring Test - Fatal编程技术网

Java Camel处理器单元/集成测试

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

我可能完全错过了一些东西,但我无法按照我的意愿测试我的路线

我得到了以下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");
            }
        };
    }
}   
您可以看到有关如何使用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;
}