本文介紹了使用camelestSupport的CAMEL單元測(cè)試,模板始終為空的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我正在使用Camel進(jìn)行一個(gè)簡(jiǎn)單的單元測(cè)試。我想要做的就是從一個(gè)文件(在資源下)中讀取JSON內(nèi)容,將其發(fā)送到Java類進(jìn)行驗(yàn)證–這就是我正在嘗試測(cè)試的方法。無(wú)論我做什么,模板(我用來(lái)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;
}
}
我面臨的問(wèn)題:
最初,結(jié)果終結(jié)點(diǎn)也始終為空。(我用FilterTest.java作為參考)。然后我不得不做了一個(gè)明確的
resultEndpoint = getMockEndpoint("mock:result");
以解決該問(wèn)題。
然后我讀到我必須覆蓋createRegistry,但我不知道如何綁定。我只是使用了我的驗(yàn)證類的名稱,但我不知道這是否正確。
但模板始終為空。null pointer exception(NPE)位于
template.sendBody("direct:start", testJson);
如有必要,也請(qǐng)指點(diǎn)我一些讀物。Apache Camel文檔鏈接到的參考代碼甚至沒有我在Setup方法中所做的Camel的啟動(dòng)。
推薦答案
我認(rèn)為您錯(cuò)過(guò)了CamelTestSupport
為您提供的許多真正有用的東西。它有自己的setUp
方法,您應(yīng)該重寫它。我相信你的測(cè)試應(yīng)該是這樣的:
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();
}
}
實(shí)際上,我會(huì)完全刪除setUp
的覆蓋,并將測(cè)試數(shù)據(jù)的讀取放入測(cè)試方法本身。這樣,數(shù)據(jù)的用途就很清楚了,您可以刪除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();
}
}
那里,簡(jiǎn)單多了。
這篇關(guān)于使用camelestSupport的CAMEL單元測(cè)試,模板始終為空的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,