2014-10-02 52 views
1

我一直在使用Apache Camel因爲3-4個月Spring 4.0.7.RELEASE 我有一個基於擴展CamelTestSupport,我在其中使用一些MockEndpoint小號幾個Camel 2.14.0 TestNG測試。 我通過重寫createRouteBuilder()方法來配置路由。從CamelTestSupport遷移到AbstractCamelTestNGSpringContextTests

現在我還需要在其中一個注入一些Spring beans,通過@Autowired註釋。 通過閱讀http://camel.apache.org/spring-testing.html的內容,我瞭解到我現在必須擴展AbstractCamelTestNGSpringContextTests,它支持@Autowired,@DirtiesContext@ContextConfiguration

雖然我理解所有的MockEndpoint s爲沒有通過getMockEndpoint()方法更容易獲得,但是通過使用@EndpointInject標註,目前尚不清楚對我來說是我該怎麼表達我的路線,因爲createRouteBuilder()沒有更多的可用。

我看到可以通過使用註釋來定義生產者和消費者,但我無法理解如何設計路線。

非常感謝社區。

回答

1

或者給定here的解決方案,您可以結合AnnotationConfigApplicationContext如果你想,而不需要額外的XML Spring配置文件來初始化一個註釋基於Spring的配置上下文中使用TestNG的幫手CamelSpringTestSupport

使用Spring註解駱駝配置bean類:

@Configuration 
public class MyConfig extends SingleRouteCamelConfiguration { 
    @Bean 
    @Override 
    public RouteBuilder route() { 
     return new RouteBuilder() { 
      @Override 
      public void configure() throws Exception { 
       from("direct:test").to("mock:direct:end"); 
      } 
     }; 
    } 
} 

TestNG的測試類擴展和CamelSpringTestSupport Spring配置MyConfigAnnotationConfigApplicationContext初始化:

public class TestNGTest extends org.apache.camel.testng.CamelSpringTestSupport { 
    @EndpointInject(uri = "mock:direct:end") 
    protected MockEndpoint errorEndpoint; 

    @Produce(uri = "direct:test") 
    protected ProducerTemplate testProducer; 

    @Override 
    protected AbstractApplicationContext createApplicationContext() { 
     return new AnnotationConfigApplicationContext(MyConfig.class); 
    }  

    @DirtiesContext 
    @Test 
    public void testRoute() throws InterruptedException { 
     // use templates and endpoints 
    } 
}