2017-02-16 9 views
0

私はいくつかのラクダルートが設定されているSpringbootアプリケーションを持っています。私はここにhttp://camel.apache.org/camel-test.html言及した別のものを試してみましたが、それは作業を取得していないようqueueEventHandler :: handleQueueEvent
にtestQueue:Springブート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のから、このルートをテストしたいです。

私はこの

@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はまだnullです。

をお勧めします。私はCamel 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

Camelテストランナーを試しましたか?

@RunWith(CamelSpringJUnit4ClassRunner.class) 

あなたがcamel-spring-boot依存関係を使用している場合、あなたはそれがセットアップキャメルに自動設定を使用していることを知っている可能性があります

CamelAutoConfiguration.java 

それはまた、あなたのテストに@EnableAutoConfigurationを追加する必要があるかもしれないことを意味します。

+0

こんにちは、CamelSpringJUnit4ClassRunnerが推奨されていませんでした方法です。私はこれのようなもので働いています。 。 'publicクラスAmqTestはRoutesBuilder createRouteBuilder(保護CamelTestSupport { @Override)は{新しいActiveMqConfigを(戻り )例外をスロールート()延びています。 } ' – pvpkiran