问题描述
限时送ChatGPT账号..我想为使用文件作为输入的 Camel 路由创建模拟测试:
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.camelponent.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apachemons.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();
}
}
这篇关于使用文件作为输入的 Camel Mock 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论