2017-02-20 25 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()); 
}} 

Это работает прекрасно. Но я должен установить заполнитель {{push.queue.name}} с помощью функции useOverridePropertiesWithPropertiesComponent. Но я хочу, чтобы это было прочитано из моего .yml-файла.
Я не могу это сделать. Может кто подскажет.

Thanks

ответ

1

Свойства обычно считываются из .properties файлов. Но вы можете написать код, который читает файл yaml в методе useOverridePropertiesWithPropertiesComponent и помещает их в возвращаемый экземпляр Properties.

0

Thank Claus.
Я получил это, сделав это

@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; 
    } 

 Смежные вопросы

  • Нет связанных вопросов^_^