有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Apache Camel模拟REST端点返回NullPointerException

我想模拟来自REST端点的响应,以便测试我的路由。我遵循《行动中的骆驼》一书,并将REST测试示例implementation改编如下:

    @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。在debug中运行时,我可以看到CamelContextnull,因此出现异常。我的预期行为是在调用端点SERVICECALL时接收纯文本响应“endpoint”

我不明白为什么会出现这种异常(我应该在某处定义mock的上下文吗?)以及如何正确实现模拟端点来测试路由


共 (0) 个答案