Apache Camel – 模拟REST端点返回NullPointerException

huangapple 未分类评论45阅读模式
英文:

Apache Camel - Mock REST endpoint returns NullPointerException

问题

以下是翻译好的内容:

我想要模拟来自REST端点的响应,以便我可以测试我的路由。我正在按照《Camel实战》这本书的内容,并将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。当我以调试模式运行时,我可以看到 CamelContextnull,因此会出现异常。我预期的行为是,当调用端点 SERVICECALL 时,应该接收到纯文本响应“endpoint”。

我对于为什么会出现这个异常感到困惑(我是否应该在某个地方定义模拟的上下文?),以及我应该如何正确实现模拟端点以测试路由感到困惑。

英文:

I would like to mock responses from a REST endpoint so that I can test my route. I am following the Camel in Action book and have adapted the REST testing example implementation as follows:

    @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();

    }

The getMockEndpoint method returns a NullPointerException. When I run in debug I can see that the CamelContext is null, hence the exception. My intended behaviour is to instead receive plain text response "endpoint" when the endpoint SERVICECALL is called.

I'm confused as to why I am getting this exception (should I define the context for the mock somewhere?) and how I should correctly implement the mock endpoint to test the route.

huangapple
  • 本文由 发表于 2020年4月6日 21:01:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/61060476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定