pvp*_*ran 1 apache-camel spring-boot camel-test
我有一个 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");
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试从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
}
Run Code Online (Sandbox Code Playgroud)
但我的 ProducerTemplate 总是null. 我尝试了 auto-wiring CamelContext,为此我收到一个异常,说它无法解析 camelContext。但这可以通过添加SpringCamelContext.class到@SpringBootTest类来解决。但我ProducerTemplate的还是null。
请建议。我正在使用 Camel 2.18 和 Spring Boot 1.4。
在支持 Spring Boot 2 的 Camel 2.22.0 及后续版本中,您可以使用以下模板来测试支持 Spring Boot 2 的路由:
@RunWith(CamelSpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = {
Route1.class,
Route2.class,
...
})
@EnableAutoConfiguration
@DisableJmx
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class RouteTest {
@TestConfiguration
static class Config {
@Bean
CamelContextConfiguration contextConfiguration() {
return new CamelContextConfiguration() {
@Override
public void beforeApplicationStart(CamelContext camelContext) {
// configure Camel here
}
@Override
public void afterApplicationStart(CamelContext camelContext) {
// Start your manual routes here
}
};
}
@Bean
RouteBuilder routeBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("direct:someEndpoint").to("mock:done");
}
};
}
// further beans ...
}
@Produce(uri = "direct:start")
private ProducerTemplate template;
@EndpointInject(uri = "mock:done")
private MockEndpoint mockDone;
@Test
public void testCamelRoute() throws Exception {
mockDone.expectedMessageCount(1);
Map<String, Object> headers = new HashMap<>();
...
template.sendBodyAndHeaders("test", headers);
mockDone.assertIsSatisfied();
}
}
Run Code Online (Sandbox Code Playgroud)
Spring Boot 区分@Configuration和@TestConfiguration。如果在顶级类上注释,则初级将替换任何现有配置,同时@TestConfiguration将在其他配置之外运行。
此外,在较大的项目中,您可能会遇到自动配置问题,因为您不能依赖 Spring Boot 2 来配置自定义数据库池或不正确的配置,或者在您具有特定目录结构且配置不在其中的情况下直接祖先目录。在这种情况下,省略@EnableAutoConfiguration注释可能更可取。为了告诉 Spring 仍然自动配置 Camel,您可以简单地传递CamelAutoConfiguration.class给中提到的类@SpringBootTest
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = {
Route1.class,
Route2.class,
RouteTest.Config.class,
CamelAutoConfiguration.class
}
Run Code Online (Sandbox Code Playgroud)
由于没有执行自动配置,Spring 不会在您的测试类中加载测试配置,也不会初始化 Camel。通过手动将这些配置添加到引导类,Spring 将为您完成。
| 归档时间: |
|
| 查看次数: |
15579 次 |
| 最近记录: |