使用@SpringBootTest进行Spring Boot和Camel测试

7

我有一个Spring Boot应用程序,使用的版本是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 也会失败 :(
有人知道这个注释对 camel 测试的影响是什么,或者如何调整它使其正常工作吗?
谢谢

2
请发布异常/错误/堆栈跟踪! - mrkernelpanic
不幸的是,这是 JUnit 断言错误,该断言未满足。 - bijesanu
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();
  }
}

我不知道为什么,但这对我不起作用?您能否请指定您在测试2中如何测试MyRoute类? - Sandeep Mandori
@Sandeep Mandori 消息发送到您正在测试的路由中定义为来源的 file:in。 然后将该消息发送到 mock:file:out 中,因为我们可以监视 mock 端点,然后 mockCamel.assertIsSatisfied(); 将检查端点是否得到满足(消息计数为 1)。 - bijesanu

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

实现将CamelSpringTestSupport的功能引入基于Spring Boot Test的测试用例。此方法允许开发人员使用常规的Spring测试约定进行应用程序/路由的测试开发。

此外,考虑添加@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)@DisableJmx(true)。第一个清除每个测试方法后的Spring上下文,这将避免在同一测试用例中的其他测试中留下任何后果(例如没有理由失败测试)。后者将禁用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();
    }
}

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接