spring - 测试休息端点 - Camel 3 + Spring Boot
问题描述
我不知道如何使用 Apache Camel 3 测试端点休息。你能帮我吗?
那是我的代码。将 xml 解组为 pojo,然后将 pojo 解组为 json 并将其发送到外部服务“my.applications.url”。我需要模拟外部响应。我该怎么做?
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。
1)向要模拟的路线步骤添加标识符/标记
.toD("{{my.applications.url}}?throwExceptionOnFailure=false").id("RequestToMock")
2)然后使用 AdviceWith将标记的步骤替换为其他内容
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 (取决于你有什么类型的测试)
@UseAdviceWith // for Spring Boot tests
@Override
public boolean isUseAdviceWith() { // for CamelTestSupport
return true;
}
推荐阅读
- spring-boot - Gitlab ci - 工件应用程序 jar 的差异
- swift - 使用可编码从 Firebase Cloud Firestore 解码集合数组
- c# - 在c#中基于字符串参数创建类实例的生产标准
- html - 如何在模板文字字符串中插入 HTML?
- android - 为什么不调用 onActivityReenter?
- cordova - 科尔多瓦插件背景下载未安装最新版本
- angular - 无法读取 Object.eval [as updateRenderer] 处未定义的属性“标题”
- python - 如何将 EXIF/其他元数据插入存储在内存缓冲区中的 JPEG 中?
- inno-setup - 从注册表中检索组合框的值,并通过覆盖安装程序第二次运行的默认值来填充它
- php - 通过电子邮件更改 MySQL 值