java - Apache Camel - 模拟 REST 端点返回 NullPointerException
问题描述
我想模拟来自 REST 端点的响应,以便测试我的路由。我正在关注 Camel in Action 这本书,并已对 REST 测试示例实现进行了如下调整:
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("jetty://http://localhost:9080/SERVICECALL")
.transform().message(m -> "endpoint")
.to("mock:endpoint");
}
};
}
@Test
public void shouldCallRedaction() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:endpoint");
mock.expectedBodiesReceived("endpoint");
mock.whenAnyExchangeReceived(e -> e.getIn().setBody("endpoint"));
String url = "http://localhost:9080/SERVICECALL";
String response = template.requestBody(url, "test", String.class);
assertEquals("endpoint", response);
assertMockEndpointsSatisfied();
}
该getMockEndpoint
方法返回一个NullPointerException
. 当我在调试中运行时,我可以看到CamelContext
is null
,因此出现异常。我的预期行为是在调用端点时接收纯文本响应“端点” SERVICECALL
。
我很困惑为什么我会得到这个异常(我应该在某处定义模拟的上下文吗?)以及我应该如何正确实现模拟端点来测试路由。
解决方案
推荐阅读
- angular - “元素”类型上不存在属性“onclick”
- kubernetes - 为什么我的 GKE 集群中的 pod 会收到对 Web 服务器根路径的重复请求?
- javascript - 可以在textarea中识别复制和粘贴文本吗
- python-3.x - Altair 绘图,显示竖线
- python - 使用 Numpy 或 Scipy 从邻接矩阵连接组件
- python-3.x - 遍历 Pandas 数据系列
- javascript - 如何使用 React Hooks 正确构建动态分页组件
- rest - Docker - 复制 JAR 文件:没有这样的文件或目录?
- scala - 具有错误累积的异步处理的函数签名
- php - 如何将选定值设置为数据库 PHP/SQL 中的下拉列表