4

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

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

@Autowired
CamelContext camelContext;

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

引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:

没有可用的“org.apache.camel.CamelContext”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。

依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

4

3 回答 3

8

尝试将CamelSpringBootRunner.class用作运行器并将@SpringBootTest注释添加到测试类。

来自 Camel 存储库的示例

更新(根据您的评论)

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

@BootstrapWith(SpringBootTestContextBootstrapper.class)

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

@RunWith(CamelSpringBootRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
@SpringBootTest(classes = MyClass.class) 
于 2017-08-25T11:59:56.163 回答
4

使用 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
于 2020-04-04T11:02:55.737 回答
3

只需启用 @EnableAutoConfiguration 即可

于 2019-05-09T09:43:07.760 回答