先决条件
问题
我使用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(在测试的类路径中是&gt;)
然后写了以下内容:
public class MyJmsTest extends CamelSpringTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
return new AnnotationConfigApplicationContext(MyFatJarRouter.class);
}
@Test
public void myTest() throws Exception {
...
}
}
在上面的示例中,不会从放置在测试文件夹中的application.properties读取配置。
如何在CamelSpringTestSupport单元测试中读取测试特定的配置文件?
答案 0 :(得分:3)
我回答可能有点迟,但有一种比黑客攻击端点更好的方法。以下解决方案使用Camel 2.16中引入的toD。我写了一个自定义组件“github”(也有官方组件),以下是我测试它的方法。请注意,我没有使用单个Camel专有注释。要注入属性,我可以使用properties
中的@SpringBootTest
属性,也可以使用Spring Boot中提供的任何其他标准技术。
请注意,我使用$simple{...}
来避免与Spring属性解析冲突。
<rant>
是的,Camel文档很糟糕!他们把它写成发行说明,其中有一个专门用于每个版本的部分,并且似乎没有更新文档以跟上最新版本(以下技术没有记录)。想象一下去一家餐馆并要求特价,只有服务员告诉他们前一天和前一周的特价,依此类推。如何对文档进行版本控制呢?
</rant>
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
public class GitHubRouteTest {
@Autowired
private CamelContext camelContext;
@Autowired
private ProducerTemplate template;
@Autowired
private GitHubClient gitHubClient;
@Test
public void testGitHubClientInvoked() throws InterruptedException {
template.sendBodyAndHeader("direct:start", "whatever",
"endpoint", "commits/test/test?username=test&password=test");
verify(gitHubClient).getCommitsForARepo(eq("test"), eq("master"), eq("test"), eq(20));
}
@SpringBootApplication
public static class TestApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(TestApplication.class)
.web(false)
.run(args);
}
@Bean
public RouteBuilder testRoute() {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.toD("github:$simple{in.header.endpoint}");
}
};
}
@Bean
public GitHubClient mockGitHubClient() {
GitHubClient mock = Mockito.mock(GitHubClient.class);
return mock;
}
}
}
答案 1 :(得分:0)
我通过使用标准弹簧单元测试解决了这个问题:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ActiveProfiles("test") // Load applicaton-test.properties in test/resources/config/application-test.properties
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) // cleanup spring context because jms broker does not exit properly
public class MyJmsTest {
private static final String MOCK_MY_ENDPOINT = "mock:myEndpoint";
@Autowired
CamelContext context;
@Autowired
ApplicationContext applicationContext;
@Autowired
ProducerTemplate producerTemplate;
@Before
public void configureMocks() throws Exception {
context.getRouteDefinition("MyRoute")
.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveByToString(".*myEndPointId.*")
.replace()
.to(MOCK_MY_ENDPOINT);
}
});
final MockEndpoint endpoint = context.getEndpoint(MOCK_MY_ENDPOINT, MockEndpoint.class);
endpoint.whenAnyExchangeReceived(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
InputStream inStream = getClass().getClassLoader().getResourceAsStream("xml/my.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
exchange.getIn().setBody(in);
}
});
}
@Test
public void synchronousCallBasic_1() throws Exception {
final MyConfiguration MyConfiguration = applicationContext.getBean(MyConfiguration.class);
final String myMessageBody =
context.getTypeConverter().convertTo(String.class, getClass().getClassLoader()
.getResourceAsStream("xml/0010_example.xml"));
final Object myResult = producerTemplate.requestBody(MyConfiguration.getActiveMqSynchronousEndpointUri(), myMessageBody);
assertThat(myResult, notNullValue());
assertThat((String)myResult, is("<example>1</example>"));
}
}
答案 2 :(得分:0)
我解决了这个问题,我找到了很多注释here,现在正确地注入了测试属性:
@font-face {
font-family: "Gotham-Book";
src: url("../Gotham_fonts/Gotham-Book.eot");
src:
url("../Gotham_fonts/Gotham-Book.woff") format("woff"),
url("../Gotham_fonts/Gotham-Book.otf") format("opentype");
}
此外,测试属性文件需要命名为application- {env} .properties,其中“env”是此处使用的配置文件。例如。对于测试,属性文件应该是application-test.properties