2017-06-02 30 views
1

即時通訊目前與駱駝的模擬組件的工作,我想測試它在現有的路線。基本上我想保留在應用程序中定義的現有路線,但在測試期間注入一些嘲笑,以驗證或至少偷看當前交易內容。如何用模擬測試現有的駱駝端點?

基礎上的文檔,並從Apache的駱駝食譜。我試着使用@MockEndpoints

,路線建設者

@Component 
public class MockedRouteStub extends RouteBuilder { 

    private static final Logger LOGGER = LoggerFactory.getLogger(MockedRouteStub.class); 

    @Override 
    public void configure() throws Exception { 
     from("direct:stub") 
      .choice() 
       .when().simple("${body} contains 'Camel'") 
        .setHeader("verified").constant(true) 
        .to("direct:foo") 
       .otherwise() 
        .to("direct:bar") 
       .end(); 

     from("direct:foo") 
      .process(e -> LOGGER.info("foo {}", e.getIn().getBody())); 

     from("direct:bar") 
      .process(e -> LOGGER.info("bar {}", e.getIn().getBody())); 

    } 

} 

這裏是我的測試(目前它的一個springboot項目):

@RunWith(SpringRunner.class) 
@SpringBootTest 
@MockEndpoints 
public class MockedRouteStubTest { 

    @Autowired 
    private ProducerTemplate producerTemplate; 

    @EndpointInject(uri = "mock:direct:foo") 
    private MockEndpoint mockCamel; 

    @Test 
    public void test() throws InterruptedException { 
     String body = "Camel"; 
     mockCamel.expectedMessageCount(1); 

     producerTemplate.sendBody("direct:stub", body); 

     mockCamel.assertIsSatisfied(); 
    } 

} 

消息計數爲0,它看起來更像@MockEndpoints不會被觸發。 此外,日誌顯示,日誌被觸發

route.MockedRouteStub : foo Camel 

我已經試過另一種方法是使用一個忠告:

... 
     @Autowired 
     private CamelContext context; 

     @Before 
     public void setup() throws Exception { 
      context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { 

       @Override 
       public void configure() throws Exception { 
        mockEndpoints(); 
       } 
      }); 
     } 

啓動日誌顯示,建議到位:

c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [direct://stub] with mock endpoint [mock:direct:stub] 

但我的測試仍然失敗,消息計數= 0.

+0

我有同樣的問題。我是這樣做的。在這裏查看我的回答 https://stackoverflow.com/questions/42275732/spring-boot-apache-camel-routes-testing/44325673#44325673 – pvpkiran

+0

您可能需要使用'@RunWith(CamelSpringRunner)'使這些駱駝註解啓用。 –

+0

@ClausIbsen,嘗試了用'CamelSpringRunner',而不是'SpringRunner'但我仍然得到0的預期消息計數同樣,登錄表示消息得到路由到直接:FOO – geneqew

回答

1

發佈回答哪些工作的設置,我有。

沒有任何改變RouteBuilder,測試會是這個樣子:

@RunWith(CamelSpringBootRunner.class) 
@SpringBootTest 
@MockEndpoints 
public class MockedRouteStubTest { 

    @Autowired 
    private ProducerTemplate producerTemplate; 

    @EndpointInject(uri = "mock:direct:foo") 
    private MockEndpoint mockCamel; 

    @Test 
    public void test() throws InterruptedException { 
     String body = "Camel"; 
     mockCamel.expectedMessageCount(1); 

     producerTemplate.sendBody("direct:stub", body); 

     mockCamel.assertIsSatisfied(); 
    } 

}