Prerequisites
-
Apache Tomcat 7
-
Spring 季4.1.5.RELEASE
-
Spring Boot 1.2.2.RELEASE
-
Apache Camel 2.15.1
Problem
我使用Spring Boot和EndpointSetup也使用的配置类 .
@SpringBootApplication
@Import({MyConfiguration.class, EndpointSetup.class})
public class MyFatJarRouter extends FatJarRouter { ... }
@Configuration
@ConfigurationProperties(prefix = "camel.route", ignoreUnknownFields = false)
public class MyConfiguration {
private List<String> brokerUrl = new ArrayList<>();
public List<String> getBrokerUrl() {return brokerUrl;}
public void setBrokerUrl(List<String> brokerUrl) {this.brokerUrl = brokerUrl;}
}
在 生产环境 中,默认情况下将从conf / application.properties中读取属性 .
我想通过CamelSpringTestSupport测试我的路线
所以我试过以下:
我在test / resources / config / application.properties下放了一个application.properties( - >在testpath的classpath中)
然后写了以下:
public class MyJmsTest extends CamelSpringTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
return new AnnotationConfigApplicationContext(MyFatJarRouter.class);
}
@Test
public void myTest() throws Exception {
...
}
}
在上面的示例中,不会从放置在测试文件夹中的application.properties读取配置 .
如何在CamelSpringTestSupport单元测试中读取测试特定的配置文件?
3 回答
我可能在回答方面有点迟,但有一种比黑客攻击 endpoints 更好的方法 . 以下解决方案使用Camel 2.16中引入的toD . 我编写了一个自定义组件"github"(没有使用单个Camel专有注释的's an official one as well), and the following is how I test it. Note that I' . 要注入属性,我可以使用
@SpringBootTest
中的properties
属性,或Spring Boot中可用的任何其他标准技术 .请注意,我正在使用
$simple{...}
来避免与Spring属性解析冲突 .<rant>
是的,Camel文档很糟糕!他们把它写成发行说明,其中有一个专门用于每个版本的部分,并且似乎没有更新文档以跟上最新版本(以下技术没有记录) . 想象一下去一家餐馆并要求特价,只有服务员告诉他们前一天和前一周的特价,依此类推 . 如何对文档进行版本控制呢?
</rant>
我通过使用标准 spring 单元测试解决了这个问题:
我解决了这个问题,我找到了很多注释here,现在正确地注入了测试属性:
此外,测试属性文件需要命名为application- .properties,其中“env”是此处使用的配置文件 . 例如 . 对于测试,属性文件应该是application-test.properties