我有一个 Springboot 应用程序,其中配置了一些 Camel 路由。
public class CamelConfig {
private static final Logger LOG = LoggerFactory.getLogger(CamelConfig.class);
@Value("${activemq.broker.url:tcp://localhost:61616}")
String brokerUrl;
@Value("${activemq.broker.maxconnections:1}")
int maxConnections;
@Bean
ConnectionFactory jmsConnectionFactory() {
PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(new ActiveMQConnectionFactory(brokerUrl));
pooledConnectionFactory.setMaxConnections(maxConnections);
return pooledConnectionFactory;
}
@Bean
public RoutesBuilder route() {
LOG.info("Initializing camel routes......................");
return new SpringRouteBuilder() {
@Override
public void configure() throws Exception {
from("activemq:testQueue")
.to("bean:queueEventHandler?method=handleQueueEvent");
}
};
}
}
我想测试从activemq:testQueue
到的这条路线queueEventHandler::handleQueueEvent
。我尝试了这里提到的不同的东西http://camel.apache.org/camel-test.html,但似乎没有让它工作。
我正在尝试做这样的事情:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {CamelConfig.class, CamelTestContextBootstrapper.class})
public class CamelRouteConfigTest {
@Produce(uri = "activemq:testQueue")
protected ProducerTemplate template;
@Test
public void testSendMatchingMessage() throws Exception {
template.sendBodyAndHeader("testJson", "foo", "bar");
// Verify handleQueueEvent(...) method is called on bean queueEventHandler by mocking
}
但我的 ProducerTemplate 始终是null
. 我尝试了 auto-wiring CamelContext
,但我得到一个异常,说它无法解析 camelContext。但这可以通过添加类SpringCamelContext.class
来解决@SpringBootTest
。但我ProducerTemplate
的还在null
。
请建议。我正在使用 Camel 2.18 和 Spring Boot 1.4。