2017-02-20 66 views
0

我想用CamelTestSupport測試我的駱駝路線。我有一個類中定義這樣CamelTestSupport從yml文件讀取佔位符

public class ActiveMqConfig{ 

@Bean 
public RoutesBuilder route() { 
    return new SpringRouteBuilder() { 
     @Override 
     public void configure() throws Exception { 
      from("activemq:{{push.queue.name}}").to("bean:PushEventHandler?method=handlePushEvent"); 
     } 
    }; 
} 

}

而且我的測試類這個樣子的

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

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

@Override 
protected Properties useOverridePropertiesWithPropertiesComponent() { 
    Properties properties = new Properties(); 
    properties.put("pim2.push.queue.name", "pushevent"); 
    return properties; 
} 

protected Boolean ignoreMissingLocationWithPropertiesComponent() { 
    return true; 
} 

@Mock 
private PushEventHandler pushEventHandler; 

@BeforeClass 
public static void setUpClass() throws Exception { 
    BrokerService brokerSvc = new BrokerService(); 
    brokerSvc.setBrokerName("TestBroker"); 
    brokerSvc.addConnector("tcp://localhost:61616"); 
    brokerSvc.setPersistent(false); 
    brokerSvc.setUseJmx(false); 
    brokerSvc.start(); 
} 

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

    return jndi; 
} 

@Test 
public void testConfigure() throws Exception { 
    template.sendBody("activemq:pushevent", "HelloWorld!"); 
    Thread.sleep(2000); 
    verify(pushEventHandler, times(1)).handlePushEvent(any()); 
}} 

這是工作完全正常我的路線。但我必須使用useOverridePropertiesWithPropertiesComponent函數設置佔位符{{push.queue.name}}。但我希望它從我的.yml文件中讀取。
我無法做到。有人可以建議。

謝謝

回答

1

屬性通常從.properties文件中讀取。但是你可以編寫一些代碼讀取useOverridePropertiesWithPropertiesComponent方法中的yaml文件,並將它們放入返回的Properties實例中。

0

謝謝克勞斯。
我這樣做的工作

@Override 
    protected Properties useOverridePropertiesWithPropertiesComponent() { 
     YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); 
     try { 
      PropertySource<?> applicationYamlPropertySource = loader.load(
       "properties", new ClassPathResource("application.yml"),null); 
      Map source = ((MapPropertySource) applicationYamlPropertySource).getSource(); 
      Properties properties = new Properties(); 
      properties.putAll(source); 
      return properties; 
     } catch (IOException e) { 
      LOG.error("Config file cannot be found."); 
     } 

     return null; 
    }