2017-02-20 12 views
0

CamelTestSupportを使用してCamel Routesをテストしようとしています。私は私のルートは、この完全に正常に動作しているこのCamelTestSupport ymlファイルのプレースホルダを読み取る

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

のようなこの

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

}

そして、私のテストクラスの外観のようなクラスで定義されています。しかし、私はプレースホルダー{{push.queue.name}}useOverridePropertiesWithPropertiesComponent関数を使って設定しなければなりません。しかし、私はそれを私の.ymlファイルから読みたいと思っています。
私はそれを行うことができません。誰かが提案することができます。

ありがとう

答えて

1

プロパティは通常、.propertiesファイルから読み取られます。しかし、yamlファイルを読み取るコードをuseOverridePropertiesWithPropertiesComponentメソッドで記述し、返される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; 
    } 
関連する問題