我有一个有效的cxfrs路线。我可以使用带有json对象的SoapUi来获取其余服务并获得响应。
我想编写单元测试,但我想使用CamelTestSupport。我的routebuilder configure()方法看起来像这样:
from(cxfrsEndpoint)
.recipientList(simple("direct:${header.operationName}"))
.setHeader(Exchange.CONTENT_LENGTH, simple("-1"));
from("direct:submitRequest")
.bean("responseBean", "checkJson")
.bean("responseBean", "createSuccessResponse");
当我从SoapUi(http://localhost:8181/cxf/myContext/submitRequest)点击url作为带有适当json字符串的POST时,我得到了一个“成功”的json。凉。
在我的单元测试中,我创建了一个覆盖的createRouteBuilder方法:
@Override
protected RouteBuilder createRouteBuilder() {
MyRouteBuilder myRouteBuilder = new MyRouteBuilder();
myRouteBuilder.setCxfrsEndpoint("direct:start");
return myRouteBuilder;
}
然后我的单元测试(我认为)看起来像这样:
@Test
public void thisIsATest() throws Exception {
MyRequest myRequest = new MyRequest();
request.setSomeProperty("Some property value");
ObjectMapper objectMapper = new ObjectMapper();
String goodJsonRequest = objectMapper.writeValueAsString(request);
String response = (String) template.requestBodyAndHeader(START_POINT, goodJsonRequest, "operationName", "submitRequest");
assertNotNull(response);
//Omitted: further assertions for content of the response json
}
好吧,当我执行单元测试时,我得到一个解组异常,原因是:
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:647)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2054)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1131)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:164)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:415)
... 74 more
它不喜欢我的json字符串。什么是真正的服务接收?
在实际路线上,我启用了blueprint.xml中的端点登录,如下所示:
<!-- RouteBuilder declarations -->
<bean id="myRouteBuilder" class="com.mycompany.MyRouteBuilder">
<property name="cxfrsEndpoint" value="cxfrs:bean:cxfrsEndpoint?bindingStyle=SimpleConsumer&loggingFeatureEnabled=true" />
</bean>
我在日志中看到的json字符串为“Payload:”与我通过template.requestBodyAndHeader(...)发送的json字符串看起来没什么不同。
我错过了什么或我在这里做错了什么?
谢谢!
答案 0 :(得分:0)
请尝试不要像这样重写您的终端:
myRouteBuilder.setCxfrsEndpoint("direct:start");
在我看来,覆盖您的端点会更改界面,而CXF现在需要XML而不是JSON(基于您的日志):
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:647)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2054)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1131)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:164)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:415)
... 74 more
您已在此处定义了端点:
<bean id="myRouteBuilder" class="com.mycompany.MyRouteBuilder">
<property name="cxfrsEndpoint" value="cxfrs:bean:cxfrsEndpoint?bindingStyle=SimpleConsumer&loggingFeatureEnabled=true" />
</bean>
我的建议是以this code为例,直接通过HTTP调用您的终端:
public void setUp() throws Exception {
super.setUp();
httpclient = HttpClientBuilder.create().build();
}
public void tearDown() throws Exception {
super.tearDown();
httpclient.close();
}
@Test
public void testGetCustomerOnlyHeaders() throws Exception {
HttpGet get = new HttpGet("http://serviceurl.com");
HttpResponse response = httpclient.execute(get);
assertEquals(200, response.getStatusLine().getStatusCode());
}
如果您有机会,请查看整个camel-cxf
项目测试,以了解如何正确测试。