2017-02-16 192 views
0

我有一個Springboot應用程序,我配置了一些駱駝路由。Spring Boot Apache 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總是空。我嘗試了Autowiring Camelcontext,爲此我得到一個異常說它無法解析camelContext。但是可以通過將SpringCamelContext.class添加到@SpringBootTest類來解決這個問題。但我的ProducerTemplate仍然爲空。

請建議。我正在使用駱駝2.18 Springboot 1.4

回答

0

這是我這究竟是怎麼最終

@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(); 
    } 
} 
+0

你能否在Git中提供完整的路由和測試代碼?謝謝。 – sunleo

1

您是否嘗試使用駱駝測試跑步者?

@RunWith(CamelSpringJUnit4ClassRunner.class) 

如果使用camel-spring-boot依賴,你可能知道它使用自動配置設置駱駝:

CamelAutoConfiguration.java 

這意味着你可能還需要添加@EnableAutoConfiguration您的測試。

+0

嗨,CamelSpringJUnit4ClassRunner已被棄用。我得到它與這樣的東西工作。 'public class AmqTest extends CamelTestSupport { @Override protected RoutesBuilder createRouteBuilder()throws Exception { return new ActiveMqConfig()。route(); }' – pvpkiran