2

我正在为 Spring Boot 应用程序中的生产 Camel 路由编写一些单元测试。为此,我使用adviceWith技术并且不希望CamelContext在建议完成之前开始。使用 Camel 2.24.2,我的测试类看起来像下面的一个片段,为了简洁起见,我省略了自动装配和测试期望等细节。

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@UseAdviceWith
public class CamelApplicationTests {
    ...
    context.getRouteDefinition("test.simple").adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("file:/home/outfolder")
                .skipSendToOriginalEndpoint()
                .to("mock:outFolder");
            }
        });
    context.start();
    ...
}

运行这个测试我得到了预期的日志

INFO  melSpringBootExecutionListener - @RunWith(CamelSpringBootRunner.class) before: class org.sample.CamelApplicationTests.testSimple
INFO  melSpringBootExecutionListener - Initialized CamelSpringBootRunner now ready to start CamelContext
INFO  spring.CamelAnnotationsHandler - Skipping starting CamelContext(s) as UseAdviceWith annotation was found and isUseAdviceWith is set to true.

现在,是时候迁移到 Camel 3 了,建议部分必须重写如下,它工作得很好

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@UseAdviceWith
public class CamelApplicationTests {
    ...
    AdviceWithRouteBuilder.adviceWith(context, "test.simple", route ->
        route.interceptSendToEndpoint("file:/home/outfolder")
            .skipSendToOriginalEndpoint()
            .to("mock:outFolder"));
    context.start();
    ...
}

除了 CamelContext 会自动启动,所以我们重新启动了建议的路线

INFO  o.a.c.i.e.InternalRouteStartupManager    : Route: test.simple started and consuming from: file:///home/infolder
INFO  o.a.c.impl.engine.AbstractCamelContext   : Total 1 routes, of which 1 are started
INFO  o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.4.3 (camel) started in 0.923 seconds
INFO  org.sample.CamelApplicationTests         : Started  in 11.544 seconds (JVM running for 13.935)
INFO  o.a.c.i.engine.DefaultShutdownStrategy   : Starting to graceful shutdown 1 routes (timeout 45 seconds)
...
INFO  o.a.c.impl.engine.AbstractCamelContext   : Route: test.simple is stopped, was consuming from: file:///home/infolder
INFO  o.a.c.impl.engine.AbstractCamelContext   : Route: test.simple is shutdown and removed, was consuming from file:///home/infolder
INFO  org.apache.camel.reifier.RouteReifier    : AdviceWith route after: ...
...
INFO  o.a.c.i.e.InternalRouteStartupManager    : Route: test.simple started and consuming from: file:///home/infolder

这似乎是一个错误,或者我错过了什么?

PS
经过一番研究,我发现在 Camel 2.21 版本中已经修复了一个问题CamelSpringBootExecutionListener.java,所以我检查了分支中的文件3.4.x,它看起来不错,但是在仔细查看我的日志文件后,我注意到没有@RunWith(CamelSpringBootRunner.class)准备日志,这意味着该prepareTestInstance方法从未被调用,但我想它应该

4

1 回答 1

0

通过迁移到 Junit 5 解决了这个问题,现在我的测试类注释为

import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.apache.camel.test.spring.junit5.UseAdviceWith;
import org.springframework.boot.test.context.SpringBootTest;

@CamelSpringBootTest
@SpringBootTest
@UseAdviceWith
public class CamelApplicationTests {
    ...
}
于 2020-10-09T16:33:12.350 回答