3

私はApacheCamelを初めて使用します。ディレクトリをスキャンする簡単なルート(/ test)を作成しました。ファイルは、ディレクトリにコピーされたときに処理されます。次のルートをテストするためのラクダのユニットテストを作成する方法について誰かが考えていますか?ルートがトリガーされるように、ファイルを/testディレクトリにコピーするプロセスをモックする方法はありますか。

public void configure() {
    from( "file:/test?preMove=IN_PROGRESS" + 
          "&move=completed/${date:now:yyyyMMdd}/${file:name}" + 
          "&moveFailed=FAILED/${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.${file:ext}" )
    .process(new Processor() {
          public void process(Exchange exchange) throws IOException {
              File file = (File) exchange.getIn().getBody();
              // read file content ......                 
          }
    });
}
4

2 に答える 2

0

基本的な考え方は、エンドエンドポイントをモックして、ルートから何が出てくるかを確認できるようにすることだと思います。いくつかの方法がありますが、次のようにルートをテストできます。

public class MyRouteTest extends CamelSpringTestSupport {

    private static final String INPUT_FILE = "myInputFile.xml";

    private static final String URI_START = "direct:start";

    private static final String URI_END = "mock:end";

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new AnnotationConfigApplicationContext(CamelTestConfig.class); // this is my Spring test config, where you wire beans
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        MyRoute route = new MyRoute();
        route.setFrom(URI_START); // I have added getter and setters to MyRoute so I can mock 'start' and 'end'
        route.setTo(URI_END);
        return route;
    }

    @Test
    public void testMyRoute() throws Exception {

        MockEndpoint result = getMockEndpoint(URI_END);
        context.start();

        // I am just checking I receive 5 messages, but you should actually check the content with expectedBodiesReceived() depending on what your processor does to the those files.

        result.expectedMessageCount(5);

        // I am just sending the same file 5 times
        for (int i = 0; i < 5; i++) {
            template.sendBody(URI_START, getInputFile(INPUT_FILE));
        }        

        result.assertIsSatisfied();
        context.stop();
    }

    private File getInputFile(String name) throws URISyntaxException, IOException {
        return FileUtils.getFile("src", "test", "resources", name);
    }

2013年の問題はすでに解決されていると思いますが、これが2017年に解決する方法です。

于 2017-06-07T09:46:25.453 に答える