2017-06-02 82 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(); 
    } 

}