2016-11-10 21 views
1

私はアプリケーションでSpringブートを伴うApache Camelを使用しています。現在私は単体テストに取り組んでいます。Apache Camel Beansユニットテスト

Javaコード

  • DataRouteクラス

    from("direct:getData") 
    .routeId("getData") 
    .bean(DataService.class, "processData") 
    .marshal().json(JsonLibrary.Jackson) 
    .end(); 
    
  • ゲッター、セッターとジャクソンのtoStringメソッド

    とのDataServiceクラス

    public Data processData() { 
        return new Data("Hello World"); 
    } 
    
  • データクラスを

    private String value; 
    

ユニットテスト

  • 私はユニットに

    .bean(DataService.class, "processData") 
    

    を模擬するにはどうすればよいBaseCamelContextUnitText

    public abstract class BaseCamelContextUnitTest extends CamelTestSupport 
    { 
        @Autowired 
        private DataService dataService; 
    
        @Produce 
        private ProducerTemplate producerTemplate; 
    
        public CamelContext getCamelContext() { 
         return camelContext; 
        } 
    
        @Override 
        protected Context createJndiContext() throws Exception { 
         JndiContext context = new JndiContext(); 
         context.bind("dataService", dataService); 
         return context; 
        } 
    
        @Test 
        public void shouldProcessData() throws Exception { 
          RouteDefinition routeDef = getCamelContext().getRouteDefinition("getData"); 
          routeDef.adviceWith((ModelCamelContext) getCamelContext(), new RouteBuilder() { 
    
          @Override 
          public void configure() throws Exception { 
            from("direct:getData") 
            .pipeline("bean:dataService?method=processData"); 
          } 
        }); 
    
        getCamelContext().start(); 
    
        String responseData = "{" 
         + "\"value\":\"Unit test success\"" 
         + "}"; 
    
        Object response = producerTemplate.sendBody("direct:getData", ExchangePattern.InOut, null); 
    
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    ((InputStreamCache) response).writeTo(byteArrayOutputStream); 
    
        assertThat(new String(byteArrayOutputStream.toByteArray()), is(responseData)); 
        } 
        } 
    

"ユニットテストの成功"と言ったようなデフォルトのString変数を持つモックデータオブジェクトを返し、 "Hello World"文字列変数を持つオブジェクトの代わりにルートが模擬オブジェクトを返すかどうかをテストするテスト?

答えて

0

あなたは、あなたはいつものようにDataServiceを模擬することができます

@Component 
public class DataRoute extends RouteBuilder { 

    @Autowired 
    private DataService dataService; 

    @Override 
    public void configure() throws Exception { 
     from("direct:getData") 
     .routeId("getData") 
     .bean(dataService, "processData") 
     .marshal().json(JsonLibrary.Jackson) 
     .end(); 
    } 

} 

のようなあなたのDataRouteDataServiceをautowiredすることができます。

代わりに、ルートにbeanRef("beanName", "methodName")を使用しています。