使用文件作为输入的骆驼模拟测试 [英] Camel Mock test using file as input
本文介绍了使用文件作为输入的骆驼模拟测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为使用文件作为输入的骆驼路线创建模拟测试:
I want to create a Mock Test for a Camel route which uses as input a file:
<route id="myroute">
<from uri="file:/var/file.log&noop=true" />
. . .
</route>
到目前为止,我已经能够在路线的开头添加"direct:start"元素,并手动将文件作为正文包含::
So far I have been able to include a "direct:start" element at the beginning of the route and include manually the file as body::
context.createProducerTemplate().sendBody("direct:start", "data1-data2-data3");
我想必须有一种更好的方法来做到这一点,而无需更改原始的Spring XML文件.有什么帮助吗? 谢谢
I guess there must be a better way for doing it, without changing the original Spring XML file. Any help ? Thanks
推荐答案
您可以在from语句中使用一个属性,然后在测试中替换该属性,或者可以使用camel-test的编织支持来修改路由进行测试.
You can either use a property in your from-statement and then replace the property in the test, or you can use camel-test's weaving support to modify the route for test.
这是后者的一个例子:
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointInject;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File;
public class FooTest extends CamelTestSupport {
private static final String URI_START_ENDPOINT = "direct:teststart";
private static final String MY_ROUTE_ID = "MY_ROUTE";
private static final String URI_END = "mock:end";
@EndpointInject(uri = URI_START_ENDPOINT)
private Endpoint start;
@EndpointInject(uri = URI_END)
private MockEndpoint unmarhsalResult;
@Override
public boolean isUseAdviceWith() {
return true;
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
// return new MyRouteBuilder();
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file:/var/file.log&noop=true").routeId(MY_ROUTE_ID)
.log("Content: ${body}");
}
};
}
@Override
protected void doPostSetup() throws Exception {
context.getRouteDefinition(MY_ROUTE_ID).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith(URI_START_ENDPOINT);
weaveAddLast().to(URI_END);
}
});
context.start();
}
@Test
public void testUnmarshal() throws Exception {
unmarhsalResult.expectedMessageCount(1);
template.sendBody(URI_START_ENDPOINT, FileUtils.readFileToString(new File("src/test/resources/my_test_file.txt")));
unmarhsalResult.assertIsSatisfied();
}
}
这篇关于使用文件作为输入的骆驼模拟测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文