Spring Boot Camel 测试

Mit*_*tal 4 apache-camel spring-boot junit5 camel-test

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

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

@Autowired
CamelContext camelContext;
Run Code Online (Sandbox Code Playgroud)

但是测试失败并显示错误:

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

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

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

mgy*_*osi 7

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

来自 Camel 存储库的示例

更新(根据您的评论)

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

@BootstrapWith(SpringBootTestContextBootstrapper.class)
Run Code Online (Sandbox Code Playgroud)

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

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

  • @RunWith(CamelSpringJUnit4ClassRunner.class) @BootstrapWith(CamelTestContextBootstrapper.class) @ContextConfiguration(MyClass.class) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) @MockEndpoints("log:*") @DisableJmx(false) public class CamellainJUnit4ClassRunner受保护的 CamelContext camelContext; ......... } 让 MyClass 成为我的 SpringBoot 启动类,其中包含 main 方法和其中定义的所有其他 bean。 (2认同)