Spring 引导骆驼测试

Spring Boot Camel Testing

我需要在 Spring 启动应用程序中测试 Camel 路由。 我有 Spring boot main class,其中声明了所有必需的 bean。 我正在使用 CamelSpringJUnit4ClassRunner.class。 在 @ContextConfiguration 中添加了我的 Spring boot main class,因为它包含所有配置。我没有单独的配置 class.

我在我的测试中自动装配了 CamelContext class:

@Autowired
CamelContext camelContext;

但是测试失败并出现错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:

No qualifying bean of type 'org.apache.camel.CamelContext' available: expected at least 1 bean which qualifies as autowire candidate.

Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

尝试使用CamelSpringBootRunner.class作为跑步者并在测试中添加@SpringBootTest注释class。

Example 来自 Camel 存储库

更新(根据您的评论)

如果您将引导程序 class 更改为 SpringBootTestContextBootstrapper 那么它应该可以工作:

@BootstrapWith(SpringBootTestContextBootstrapper.class)

与您拥有的等效配置,但在这种情况下,您不需要添加 ContextConfigurationBootstrapWith 注释:

@RunWith(CamelSpringBootRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
@SpringBootTest(classes = MyClass.class) 

只要启用@EnableAutoConfiguration 就可以了

使用 Camel 3.1 Spring Boot 2.2.5 和 JUnit5,同时还设置测试应用程序属性:

@CamelSpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = "spring.cloud.consul.enabled=false")
public class CamelRouteTest {

  @Autowired
  private TestRestTemplate restTemplate;

  @Autowired
  private CamelContext camelContext;

  @EndpointInject("mock:bean:userService")
  private MockEndpoint mockUserService;

  private User user;

  @BeforeEach
  public void setUp() throws Exception {
    AdviceWithRouteBuilder.adviceWith(camelContext, "getUsersRoute", a -> {
      a.mockEndpointsAndSkip("bean:userService*");
    });

    user = new User();
    user.setId(1);
    user.setName("Jane");

    mockUserService.returnReplyBody(constant(new User[] {user}));
  }

  @Test
  public void callsRestWithMock() {
    ResponseEntity<User[]> response = restTemplate.getForEntity("/rest/users", User[].class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    User[] s = response.getBody();
    assertThat(s).contains(user);
  }

  @Test
  public void callsDirectRouteWithMock() throws Exception {
    User[] users = DefaultFluentProducerTemplate.on(camelContext)
        .to("direct:getusers")
        .request(User[].class);
    assertThat(users).contains(user);
  }

  @Test
  public void camelStarts() {
    assertEquals(ServiceStatus.Started, camelContext.getStatus());
    assertThat(camelContext.getRoutes()).hasSizeGreaterThan(0);
  }
}

假设 RouteBuilder:

@Component
public class CamelRouter extends RouteBuilder {

  @Value("${server.port}")
  private int serverPort;

  @Override
  public void configure() throws Exception {
    restConfiguration()
        .contextPath("/rest")
        .component("servlet")
        .apiContextPath("/api-doc")
        .port(serverPort)
        .bindingMode(RestBindingMode.json)
        .dataFormatProperty("prettyPrint", "true");

    rest("/users")
        .consumes("application/json")
        .produces("application/json")
        .get()
        .outType(User[].class).to("direct:getusers");

    from("direct:getusers").routeId("getUsersRoute")
        .log("Get users")
        .to("bean:userService?method=listUsers");
  }
}

application.yml:

camel:
  component:
    servlet:
      mapping:
        context-path: /rest/*
  springboot:
    name: MyCamel