【问题标题】:Spring boot and camel testing with @SpringBootTest使用 @SpringBootTest 进行 Spring Boot 和 Camel 测试
【发布时间】:2017-12-14 23:01:08
【问题描述】:

我有 Spring Boot 应用程序,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 时也没有注释它也会失败:(

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

谢谢

【问题讨论】:

  • 发布异常/错误/堆栈跟踪!
  • 不幸的错误是junit assert错误,那个assert不满足

标签: java spring-boot apache-camel spring-boot-test spring-camel


【解决方案1】:

这是最后的工作:

@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 消息被发送到您正在测试的路由中定义的file:in 作为发件人。消息是最后在mock:file:out 中发送,因为我们可以监控模拟端点,然后mockCamel.assertIsSatisfied(); 将检查端点是否满足字段(消息计数为 1)
【解决方案2】:

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

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

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

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

official docs 提供了有关使用 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();
    }
}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2017-07-05
    • 2014-08-15
    • 1970-01-01
    • 2018-05-25
    • 2016-07-01
    相关资源
    最近更新 更多