2016-05-20 3 views
0

CBR用のシンプルキャメルテストケースを作成するにはどうすればいいですか?Content Based Routingでキャメルテストを書くための最良の方法

<route id="_route1"> 
    <from id="_to1" uri="file:/home/user/data/eip-demo/in?noop=true"/> 
    <choice id="_choice1"> 
     <when id="_when1"> 
      <simple>${header.CamelFileName} regex '^.*xml$'</simple> 
      <to id="_to2" uri="file:/home/user/data/eip-demo/xml"/> 
     </when> 
     <when id="_when2"> 
      <simple>${header.CamelFileName} regex '^.*txt$'</simple> 
      <to id="_to3" uri="file:/home/user/data/eip-demo/txt"/> 
     </when> 
     <otherwise id="_otherwise1"> 
      <log id="_log1" message="File unknown!"/> 
      <to id="_to2" uri="file:/tmp"/> 
     </otherwise> 
    </choice> 
</route> 

このルートをテストする最も正しい方法は何ですか。私は、ファイル:componentを置き換え、テストケース内のファイルを生成できると思います。ただし、IDE(Jboss Developer Studio)から直接ルートを実行することはできません。テストする必要があるようなルートをコーディングするのに最も正しいアプローチは何ですか?

UPDATE:私は少しのJBoss Developer Studioにによって作成されたラクダのテストを洗練しました:

public class CamelContextXmlTest extends CamelSpringTestSupport { 

    // TODO Create test message bodies that work for the route(s) being tested 
    // Expected message bodies 
    protected Object[] expectedBodies = { "<something id='1'>expectedBody1</something>", 
      "textfile" }; 
    protected Object[] name = { "test.xml", 
    "test.txt" }; 

    // Templates to send to input endpoints 

    @Produce(uri = "file:/home/data/eip-demo/in?noop=true") 
    protected ProducerTemplate inputEndpoint; 

    // Mock endpoints used to consume messages from the output endpoints and then perform assertions 
    @EndpointInject(uri = "mock:output") 
    protected MockEndpoint outputEndpoint; 

    @EndpointInject(uri = "mock:output2") 
    protected MockEndpoint output2Endpoint; 

    @EndpointInject(uri = "mock:output3") 
    protected MockEndpoint output3Endpoint; 

    @Test 
    public void testCamelRoute() throws Exception { 
     // Create routes from the output endpoints to our mock endpoints so we can assert expectations 

     context.addRoutes(new RouteBuilder() { 
      @Override 
      public void configure() throws Exception { 
       from("file:/home/user/data/eip-demo/xml").to(outputEndpoint); 

       from("file:/home/user/data/eip-demo/txt").to(output2Endpoint); 
      } 
     }); 

     // Define some expectations 

     // TODO Ensure expectations make sense for the route(s) we're testing 
     //outputEndpoint.expectedBodiesReceivedInAnyOrder(expectedBodies); 

     // Send some messages to input endpoints 
     int index=0; 
     for (Object expectedBody : expectedBodies) { 
      inputEndpoint.sendBodyAndHeader(expectedBody,"CamelFileName",name[index]); 
      index++; 
     } 

     outputEndpoint.expectedMessageCount(1); 
     output2Endpoint.expectedMessageCount(1); 

     // Validate our expectations 
     assertMockEndpointsSatisfied(); 
    } 

    @Override 
    protected ClassPathXmlApplicationContext createApplicationContext() { 
     return new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml"); 
    } 

} 

は、それは良い解決策であるか、それを向上させることができますか?あなたがここに必要なもの

答えて

0

はAdviseWithです: http://camel.apache.org/advicewith.html

あなたは単に/交換する場で自分のエンドポイントを変更したり、単にあなたのCBRの各条件の後にその場でモックを追加する必要があります。もしあなたがそれを行えなかったなら、私に知らせてください。そして私はあなたのためにこの仕事をするいくつかの実際のコードについて助けます。

R.

関連する問題