2017-02-16 4 views
0

Ich habe eine Springboot-Anwendung, wo ich einige Kamelrouten konfiguriert habe.Spring Boot Apache Camel Routes testen

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"); 
      } 
    }; 
} 

}

Ich möchte von activemq diese Route testen: heißt test auf queueEventHandler :: handleQueueEvent
ich verschiedene Dinge ausprobiert, hier http://camel.apache.org/camel-test.html erwähnt, aber scheint nicht funktioniert zu bekommen.

Ich versuche, so etwas wie dieses

@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 

} 

Aber mein ProducerTemplate ist immer null zu tun. Ich habe Autowiring Camelcontext versucht, für das ich eine Ausnahme bekomme, die besagt, dass es camelContext nicht auflösen kann. Dies kann jedoch durch Hinzufügen von SpringCamelContext.class zu @ SpringBootTest-Klassen gelöst werden. Aber mein ProducerTemplate ist immer noch null.

bitte vorschlagen. Ich benutze Camel 2.18 Springboot 1.4

Antwort

0

ist, wie ich tat dies schließlich

@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

Konnten Sie gesamte Route und Testcode in Git zur Verfügung stellen? Danke. – sunleo

1

Haben Sie versucht, Camel Test Runner zu verwenden?

@RunWith(CamelSpringJUnit4ClassRunner.class) 

Wenn Sie camel-spring-boot Abhängigkeit verwenden, können Sie wissen, dass es die automatische Konfiguration einrichten Camel verwendet:

CamelAutoConfiguration.java 

Es bedeutet, dass Sie auch benötigen @EnableAutoConfiguration zu Ihrem Test hinzuzufügen. Diese

+0

Hallo, CamelSpringJUnit4ClassRunner ist veraltet. Ich habe es mit so etwas arbeiten lassen. 'Öffentliche Klasse AmqTest erweitert CamelTestSupport { @Override geschützt RoutesBuilder createRouteBuilder() löst Exception aus { return new ActiveMqConfig(). Route(); } ' – pvpkiran