使用camelestSupport的CAMEL单元测试,模板始终为空
我正在使用Camel进行一个简单的单元测试。我想要做的就是从一个文件(在资源下)中读取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;
}
}
我面临的问题:
最初,结果终结点也始终为空。(我用FilterTest.java作为参考)。然后我不得不做了一个明确的
resultEndpoint = getMockEndpoint("mock:result");
以解决该问题。
然后我读到我必须覆盖createRegistry,但我不知道如何绑定。我只是使用了我的验证类的名称,但我不知道这是否正确。
但模板始终为空。null pointer exception(NPE)位于
template.sendBody("direct:start", testJson);
如有必要,也请指点我一些读物。Apache Camel文档链接到的参考代码甚至没有我在Setup方法中所做的Camel的启动。
解决方案
我认为您错过了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();
}
}
那里,简单多了。
相关文章