Spring Boot Apache Camel路由测试 [英] Spring Boot Apache Camel Routes testing

查看:469
本文介绍了Spring Boot Apache Camel路由测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Springboot应用程序,其中配置了一些骆驼路线.

I have a Springboot application, where I have some camel routes configured.

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 ,但似乎无法正常工作.

I want to test this route from activemq:testQueue to queueEventHandler::handleQueueEvent
I tried different things mentioned here http://camel.apache.org/camel-test.html, but doesn't seem to get it working.

我正在尝试做这样的事情

I am trying to do something like this

@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.我尝试了Autowiring Camelcontext,为此我遇到了一个异常,说它无法解析camelContext.但这可以通过将SpringCamelContext.class添加到@SpringBootTest类来解决.但是我的ProducerTemplate仍然为空.

But my ProducerTemplate is always null. I tried Autowiring Camelcontext, for which I get an exception saying It cannot resolve camelContext. But that can be resolved by adding SpringCamelContext.class to @SpringBootTest classes. But my ProducerTemplate is still null.

请提出建议.我正在使用Camel 2.18 Springboot 1.4

please suggest. I am using Camel 2.18 Springboot 1.4

推荐答案

这是我最终做到的方式

@RunWith(SpringRunner.class)
public class CamelRouteConfigTest extends CamelTestSupport {

    private static final Logger LOG = LoggerFactory.getLogger(CamelRouteConfigTest.class);
    private static BrokerService brokerSvc = new BrokerService();

    @Mock
    private QueueEventHandler queueEventHandler;

    @BeforeClass
    //Sets up a embedded broker.
    public static void setUpBroker() throws Exception {
        brokerSvc.setBrokerName("TestBroker");
        brokerSvc.addConnector("tcp://localhost:61616");
        brokerSvc.setPersistent(false);
        brokerSvc.setUseJmx(false);
        brokerSvc.start();
    }

    @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {
        return new CamelConfig().route();
    }

    // properties in .yml has to be loaded manually. Not sure of .properties file
    @Override
    protected Properties useOverridePropertiesWithPropertiesComponent() {
        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        try {
            PropertySource<?> applicationYamlPropertySource = loader.load(
                "properties", new ClassPathResource("application.yml"),null);// null indicated common properties for all profiles.
            Map source = ((MapPropertySource) applicationYamlPropertySource).getSource();
            Properties properties = new Properties();
            properties.putAll(source);
            return properties;
        } catch (IOException e) {
            LOG.error("application.yml file cannot be found.");
        }

        return null;
    }

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry jndi = super.createRegistry();
        MockitoAnnotations.initMocks(this);
        jndi.bind("queueEventHandler", queueEventHandler);

        return jndi;
    }

    @Test
    // Sleeping for a few seconds is necessary, because this line template.sendBody runs in a different thread and
    // CamelTest takes a few seconds to do the routing.
    public void testRoute() throws InterruptedException {
        template.sendBody("activemq:productpushevent", "HelloWorld!");
        Thread.sleep(2000);
        verify(queueEventHandler, times(1)).handleQueueEvent(any());
    }

    @AfterClass
    public static void shutDownBroker() throws Exception {
        brokerSvc.stop();
    }
}

这篇关于Spring Boot Apache Camel路由测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆