【问题标题】:Apache Camel with Spring boot unit test setup带有 Spring Boot 单元测试设置的 Apache Camel
【发布时间】:2021-02-04 23:15:13
【问题描述】:

我正在尝试在一个非常基本的路线上进行单元测试的示例设置,到目前为止我尝试过的所有条目都没有让我将 CamelContext 自动连接。

我的路线有这个,下面是单元测试。

public class SampleRouter1 extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:start") // a real entry will be jms:queue:testqueue
            .log("Direct Start Init")
            .process(new SampleProcessor1())
            .to("mock:output");
    }
}

单元测试

@RunWith(CamelSpringBootRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
public class SampleRouter1Test1 {
    @Autowired
    protected CamelContext camelContext; // never gets wired in

编辑添加我正在自动装配 UT 中的上下文。

运行单元测试的异常是: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)}

还有一个问题是,这是为了测试 SampleRouter 还是您只是对流程类和任何其他支持类进行单元测试?或者您是否使用以下内容对其进行更改,以便将消息传递给虚假的直接队列?

AdviceWith.adviceWith(camelContext, "jms:queue:testqueue", a -> { a.replaceFromWith("direct:start"); });

【问题讨论】:

  • 如果在SampleRouter1Test1 类的camelContext 字段中添加@Autowired 注释会怎样?
  • 所以我确实有那个``` @Autowired public CamelContext camelContext; ``` 但它总是 null 或者它大喊它无法弄清楚如何自动连接它
  • 这能回答你的问题吗? Spring Boot Apache Camel Routes testing
  • 嗨罗曼,至少不是完全没有。我尝试使用 CamelAutoConfiguration 但它似乎不想自动连接该对象。并且从我看到的 UT 正在重建路线,这不完全是 id 喜欢做的事情。我喜欢发送到路由并让代码以这种方式流动而不是在测试类中重做

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


【解决方案1】:

因此,通过向我建议的链接,我得到了使用以下内容的单元测试:

Spring 启动 2.4.2 / Apache Camel 3.7.2

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-test-spring-junit5</artifactId>
            <version>3.7.2</version>
        </dependency>

我需要的单元测试注释:

@CamelSpringBootTest
@SpringBootTest // (webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource
// TODO: Might need the dirty context here . I have not ran into that issue just yet. 
class SampleRouter1Test {

    @Autowired
    private CamelContext camelContext;

    @BeforeEach
    public void setUp() throws Exception {
//        This is to allow you to test JMS queues by replacing their JMS:Queue: entry
//        Also the "Test Router 1" is what ever you named your router via .id().
//        AdviceWith.adviceWith(camelContext, "Test Router 1", a -> {
//            a.replaceFromWith("direct:start2");
//        });

    }

    @Produce("direct:start")
    private ProducerTemplate template;

    @EndpointInject("mock:output")
    private MockEndpoint mockDone;

    @Test
    public void t1() throws Exception {
        template.sendBodyAndHeaders("Testing", new HashMap<>());

        mockDone.expectedMessageCount(1);
    }
}

我不需要任何其他东西来自动装配上面的弹簧豆。还注意到,如果您有更多 JMS 路由,您可能需要更改它们的条目以避免启用 ActiveMQ(或哪个 jms 客户端)。

示例路由器:

@Component
public class SampleRouter1 extends RouteBuilder {

    @Autowired
    private SampleProcessor1 sampleProcessor1;

    @Override
    public void configure() throws Exception {
        from("direct:start")
                .id("Test Router 1")
                .log("Direct Start Init")
                .process(sampleProcessor1)
                .to("mock:output");
    }

}

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 2017-07-05
    • 2018-12-13
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 2019-04-10
    • 2018-01-17
    • 2016-05-28
    相关资源
    最近更新 更多