测试调用HTTP服务器的骆驼路由

时间:2020-09-15 14:12:45

标签: spring-boot apache-camel

在骆驼语境中,我的路线定义如下:

(?<=TITLE\|prefix=)[\w]+|(?<=[\w]+\|suffix=)[\w]+

路由运行良好,并可以拨打服务器。我想测试此路由并检查标题,但是我有问题。

这是一个春季项目,所以我的测试类如下:

 from("direct:getPets")
           .routeId("getPets")
          .to("http://localhost:4321/pets")

交换失败并返回空指针。

@RunWith(CamelSpringBootRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.After_EACH_TEST_METHO)

@UseAdviceWith
public class TestRestEndpoint {

  @Autowired
  private CamelContext camelContext;

  @Produce(uri = "direct:getPets")
  private ProducerTemplate callServer;

  @EndpointInject(uri = "mock:catchTestEndpoint")


  @Test
  public void should_return_json() throws Exception {
  
   camelContext.getRouteDefinitions("getPets").adviceWith(camelContext, new AdviceWithRouteBuilder() {
        @Override
    public void configure() throws Exception {
       weaveAddLast().to("mock:catchTestEndpoint");
        }
          });

    camelContext.start();

    callServer.sendBody("");
    mockEndpoint.expectedMessageCount(1);
    mockEndpoint.assertIsSatisfied();

}

}

1 个答案:

答案 0 :(得分:0)

听起来像您附加的模拟音是null

如果问题中的代码确实是您的代码,则可能是因为端点URI中有错字。

请注意t中“捕获”和“测试”之间多余的mock:catchtTestEndpoint