具有 Spring 引导单元测试设置的 Apache Camel
Apache Camel with Spring boot unit test setup
我正在尝试在非常基本的路线上进行单元测试的示例设置,到目前为止我尝试过的所有条目都没有让我自动连接 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 还是您只是对进程 class 和任何其他支持 classes 进行单元测试?或者您是否使用以下内容来更改它,以便您可以将消息传递到伪造的直接队列?
AdviceWith.adviceWith(camelContext, "jms:queue:testqueue", a -> { a.replaceFromWith("direct:start"); });
因此,根据向我建议的 link,我进行了以下单元测试:
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);
}
}
我不需要任何其他东西来自动装配 spring 以上的 bean。还注意到如果您有更多的 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");
}
}
我正在尝试在非常基本的路线上进行单元测试的示例设置,到目前为止我尝试过的所有条目都没有让我自动连接 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 还是您只是对进程 class 和任何其他支持 classes 进行单元测试?或者您是否使用以下内容来更改它,以便您可以将消息传递到伪造的直接队列?
AdviceWith.adviceWith(camelContext, "jms:queue:testqueue", a -> { a.replaceFromWith("direct:start"); });
因此,根据向我建议的 link,我进行了以下单元测试:
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);
}
}
我不需要任何其他东西来自动装配 spring 以上的 bean。还注意到如果您有更多的 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");
}
}