7

我有 spring boot 应用程序,spring boot 版本为 1.5.8camel 2.20.1

简单路线:

@Component
public class MyRoute extends RouteBuilder {

  public static final String IN = "file://in";

  public static final String OUT = "file://out";

  @Override
  public void configure() throws Exception {
    from(IN).routeId("myId").to(OUT);
  }
}

和简单的测试:

//@SpringBootTest
public class MyRouteTest extends CamelTestSupport {


      @Produce(uri = MyRoute.IN)
      private ProducerTemplate producerTemplate;

      @EndpointInject(uri = "mock:file:out")
      private MockEndpoint mockEndpointOut;

      @Override
      public String isMockEndpoints() {
        return "*";
      }

      @Test
      public void simpleTest() throws Exception {
        mockEndpointOut.expectedMessageCount(1);
        producerTemplate.sendBody("Test");
        mockEndpointOut.assertIsSatisfied();
      }

      @Override
      protected RoutesBuilder createRouteBuilder() throws Exception {
        return new MyRoute();
      }

    }

当我运行这个测试时,它运行良好,我收到一条消息并且端点很满意。如果我添加@SpringBootTest注释,测试失败?为什么 ?当我运行 maven clean install 时也没有注释它也会失败:(

任何人都知道这个注释对骆驼测试有什么作用,或者我该如何调整它以使其工作?

谢谢

4

2 回答 2

11

这是最后的工作:

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

  @Autowired
  private ProducerTemplate producerTemplate;

  @EndpointInject(uri = "mock:file:out")
  private MockEndpoint mockCamel;

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

    producerTemplate.sendBody("file:in", body);

    mockCamel.assertIsSatisfied();
  }
}
于 2017-12-14T15:01:08.617 回答
6

尝试添加注释@RunWith(CamelSpringBootRunner.class)。根据文档

将功能引入CamelSpringTestSupport基于 Spring Boot Test 的测试用例的实现。这种方法允许开发人员使用用于测试开发的典型 Spring Test 约定为他们的基于 Spring Boot 的应用程序/路由实现测试。

另外,考虑添加@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)@DisableJmx(true)。第一个在每个测试方法之后清除 Spring Context,这将避免在同一个测试用例中其他测试留下的任何后果(比如显然没有理由的测试失败)。

后者将禁用 JMX,因为您在测试中不需要它。

官方文档有更多关于使用 Spring Boot 运行 Apache Camel 的信息。这里有一个示例摘录:

@ActiveProfiles("test")
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(true)
public class MyRouteTest extends CamelTestSupport {

    @Autowired
    private CamelContext camelContext;

    @Override
    protected CamelContext createCamelContext() throws Exception {
        return camelContext;
    }

    @EndpointInject(uri = "direct:myEndpoint")
    private ProducerTemplate endpoint;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        RouteDefinition definition = context().getRouteDefinitions().get(0);
        definition.adviceWith(context(), new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                onException(Exception.class).maximumRedeliveries(0);
            }
        });
    }

    @Override
    public String isMockEndpointsAndSkip() {
            return "myEndpoint:put*";
    }

    @Test
    public void shouldSucceed() throws Exception {
        assertNotNull(camelContext);
        assertNotNull(endpoint);

        String expectedValue = "expectedValue";
        MockEndpoint mock = getMockEndpoint("mock:myEndpoint:put");
        mock.expectedMessageCount(1);
        mock.allMessages().body().isEqualTo(expectedValue);
        mock.allMessages().header(MY_HEADER).isEqualTo("testHeader");
        endpoint.sendBodyAndHeader("test", MY_HEADER, "testHeader");

        mock.assertIsSatisfied();
    }
}

希望有帮助。

于 2017-12-14T11:00:44.493 回答