测试休息端点 - Camel 3 + Spring Boot [英] Test rest endpoint - Camel 3 + Spring Boot
问题描述
我不知道如何使用 Apache Camel 3 测试端点 rest.你能帮我吗?
I don't know how to test an endpoint rest with Apache Camel 3. Can you help me?
这是我的代码.将 xml 解组为 pojo,然后将 pojo 解组为 json 并将其发送到外部服务my.applications.url".我需要模拟外部响应.我该怎么做?
That's my code. unmarshal a xml to pojo, then pojo to json and send it to an external service "my.applications.url". I need to mock the external response. How can i do it?
from("direct:my-application")
.id("my-application")
.log("app: ${body}")
.log("country: ${headers.country}")
.unmarshal(jaxbDataFormat).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
ApplicationInput bodyIn = (ApplicationInput) exchange
.getIn().getBody();
exchange.getIn().setBody(bodyIn);
}
}).setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.POST).marshal().json(JsonLibrary.Jackson)
.toD("{{my.applications.url}}?throwExceptionOnFailure=false") //<--- I need to mock in in test
.choice()
.when((header("CamelHttpResponseCode").isEqualTo("200")))
.unmarshal().json(JsonLibrary.Jackson, NCCLResponse.class)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
int responseCode = in.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
myResponse response = (myResponse) exchange.getIn().getBody();
//create response
myApplicationOutput output = createResponseOk(responseCode, response);
exchange.getIn().setBody(output);
}
})
.otherwise()
.unmarshal().json(JsonLibrary.Jackson, myResponse.class)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
int responseCode = in.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
myResponse response = (myResponse) exchange.getIn().getBody();
//create response
ErrorResponse output = createResponseError(responseCode, response);
exchange.getIn().setBody(output);
}
})
.end();```
推荐答案
如果你想在 Camel 路由测试中模拟这个调用,你可以使用 AdviceWith.
If you want to mock this call in a Camel route test, you can use AdviceWith.
1) 向要模拟的路线步骤添加标识符/标记
1) Add an identifier/marker to the route step you want to mock
.toD("{{my.applications.url}}?throwExceptionOnFailure=false").id("RequestToMock")
2) 然后使用 AdviceWith 用别的东西替换标记的步骤
2) Then use AdviceWith to replace the marked step with something else
context.getRouteDefinition("yourRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("RequestToMock") // <-- same identifier
.replace()
.setBody(simple("resource:classpath:TestResponse.json"));
}
});
3) 告诉 Camel 您的测试使用 AdviceWith(取决于您的测试类型)
3) Tell Camel that your test uses AdviceWith (depending what type of test you have)
@UseAdviceWith // for Spring Boot tests
@Override
public boolean isUseAdviceWith() { // for CamelTestSupport
return true;
}
这篇关于测试休息端点 - Camel 3 + Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!