使用cametestsupport进行camel单元测试,模板始终为null
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用cametestsupport进行camel单元测试,模板始终为null相关的知识,希望对你有一定的参考价值。
我正在用骆驼做一个简单的单元测试。我想做的就是从文件中读取json(在资源下),将其发送到java类进行验证 - 这是我尝试测试的路径。无论我做什么,模板(我用来sendBody(json)的模板总是为空。这是我的代码
public class RouteTests extends CamelTestSupport {
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri = "direct:start")
protected ProducerTemplate template;
@Autowired
JSONObject testJson;
@Before
public void setUp() throws Exception {
try {
final ObjectMapper objectmapper = new ObjectMapper();
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final InputStream stream = loader.getResourceAsStream("test.json");
testJson = new JSONObject ((Map)objectmapper.readValue(stream, Map.class));
//start camel
context = new DefaultCamelContext();
context.addRoutes(createRouteBuilder());
context.start();
} catch (IOException e) {
}
}
@Test
public void testSendMatchingMessage() throws Exception {
// resultEndpoint.expectedBodiesReceived(expectedBody);
resultEndpoint = getMockEndpoint("mock:result");
// resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
resultEndpoint.expectedMessageCount(1);
template.sendBody("direct:start", testJson);
resultEndpoint.assertIsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start")
.filter().method(ValidationProcessor.class, "validate")
.to("mock:result");
}
};
}
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
jndi.bind("ValidationProcessor", new ValidationProcessor",());
return jndi;
}
}
我遇到的问题:1。最初结果终点也始终为空。 (我用http://svn.apache.org/repos/asf/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java作为参考)。然后我必须做一个明确的
resultEndpoint = getMockEndpoint("mock:result");
解决这个问题。
- 然后我读到我必须覆盖createRegistry但我不知道如何绑定。我刚刚使用了验证类的名称,但我不知道这是否正确。
但模板始终为空。 NPE是在
template.sendBody("direct:start", testJson);
我一直试图解决这个问题很长一段时间,我终于求助于你们。真累了:(
如有必要,还请指点一些阅读。 apache camel docs链接到的参考代码甚至没有我在setUp方法中执行的camel的启动。甚至不确定是否需要它。请帮忙 !
Ananth
我想你错过了CamelTestSupport
为你做的很多真正有用的东西。它有自己的setUp
方法,你应该覆盖。我相信你的测试应该看起来像这样:
public class RouteTests extends CamelTestSupport {
private JSONObject testJson;
@Override
public void setUp() throws Exception {
// REALLY important to call super
super.setUp();
ObjectMapper objectmapper = new ObjectMapper();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream stream = loader.getResourceAsStream("test.json");
testJson = new JSONObject(objectmapper.readValue(stream, Map.class));
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("direct:start")
.filter().method(ValidationProcessor.class, "validate")
.to("mock:result");
}
};
}
@Test
public void testSendMatchingMessage() throws Exception {
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedMessageCount(1);
template.sendBody("direct:start", testJson);
resultEndpoint.assertIsSatisfied();
}
}
实际上我会完全删除setUp
的覆盖,并将测试数据的读取放入测试方法本身。然后很清楚数据的用途是什么,你可以消除testJson
字段。
public class RouteTests extends CamelTestSupport {
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("direct:start")
.filter().method(ValidationProcessor.class, "validate")
.to("mock:result");
}
};
}
@Test
public void testSendMatchingMessage() throws Exception {
ObjectMapper objectmapper = new ObjectMapper();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream stream = loader.getResourceAsStream("test.json");
JSONObject testJson = new JSONObject(objectmapper.readValue(stream, Map.class));
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedMessageCount(1);
template.sendBody("direct:start", testJson);
resultEndpoint.assertIsSatisfied();
}
}
在那里,简单得多。
尝试从驼峰上下文创建产品模板,如下所示:
public class RouteTests extends CamelTestSupport {
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
protected ProducerTemplate template;
@Autowired
JSONObject testJson;
@Before
public void setUp() throws Exception {
try {
final ObjectMapper objectmapper = new ObjectMapper();
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final InputStream stream = loader.getResourceAsStream("test.json");
testJson = new JSONObject ((Map)objectmapper.readValue(stream, Map.class));
//start camel
context = new DefaultCamelContext();
context.addRoutes(createRouteBuilder());
context.start();
template = context.createProducerTemplate();
} catch (IOException e) {
}
}
@Test
public void testSendMatchingMessage() throws Exception {
//resultEndpoint.expectedBodiesReceived(expectedBody);
resultEndpoint = getMockEndpoint("mock:result");
//resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
resultEndpoint.expectedMessageCount(1);
template.sendBody("direct:start", testJson);
resultEndpoint.assertIsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start")
.filter().method(ValidationProcessor.class, "validate")
.to("mock:result");
}
};
}
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
jndi.bind("ValidationProcessor", new ValidationProcessor());
return jndi;
}
}
所以在你的setUp()中添加:
template = context.createProducerTemplate();
并删除@Produce(uri =“direct:start”)
以上是关于使用cametestsupport进行camel单元测试,模板始终为null的主要内容,如果未能解决你的问题,请参考以下文章
使用状态码 405 获取 org.apache.camel.component.http.HttpOperationFailedException