首页 > 解决方案 > 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. 当我在调试中运行时,我可以看到CamelContextis null,因此出现异常。我的预期行为是在调用端点时接收纯文本响应“端点” SERVICECALL

我很困惑为什么我会得到这个异常(我应该在某处定义模拟的上下文吗?)以及我应该如何正确实现模拟端点来测试路由。

标签: javaapache-camel

解决方案


推荐阅读