Spring 使用@SpringBootTest 启动和骆驼测试

Spring boot and camel testing with @SpringBootTest

我有 spring 启动应用程序,spring 版本 1.5.8 启动,还有 camel 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 时也没有注释它也失败了:(

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

感谢

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

An implementation bringing the functionality of CamelSpringTestSupport to Spring Boot Test based test cases. This approach allows developers to implement tests for their Spring Boot based applications/routes using the typical Spring Test conventions for test development.

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

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

official docs 有更多关于 运行 Apache Camel with Spring Boot 的信息。这里有一个例子摘录:

@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();
    }
}

希望对您有所帮助。

这是最后起作用的:

@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();
  }
}