【问题标题】:Getting error when testing spring rest async controller测试弹簧休息异步控制器时出错
【发布时间】:2020-05-26 14:49:47
【问题描述】:

控制器

    @PostMapping("/endpoint/group/move")
    public DeferredResult<ResponseEntity<List<Endpoint>>> moveEndpointGroup(@RequestBody List<String> EndpointIds,
            @RequestParam("from") String fromGroupId, @RequestParam("to") String toGroupId)
            throws ResourceNotFoundException, ExecutionException {
        DeferredResult<ResponseEntity<List<Endpoint>>> result = new DeferredResult<>();
        if (EndpointIds != null && !EndpointIds.isEmpty()) {
            CompletableFuture.supplyAsync(() -> EndpointService.moveEndpoints(EndpointIds, fromGroupId, toGroupId), executor)
                    .whenComplete((movedEndpointsList, ex) -> {
                        if (ex != null) {
                            throw new RuntimeException(ex);
                        }
                        if (movedEndpointsList.size() == EndpointIds.size()) {
                            result.setResult(ResponseEntity.status(HttpStatus.OK).body(movedEndpointsList));
                        } else {
                            result.setResult(ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(movedEndpointsList));
                        }
                    });
        }
        return result;
    }

我已经写了以下测试方法,它不起作用。当我运行此测试时,我得到处理程序的异步结果未在指定的 timeToWait 期间设置。

有人可以帮我解决我做错的地方吗?

    @Test
    public void testMoveEndpointGroup_WhenSuccess() throws Exception {

        List<Endpoint> EndpointList = Arrays.asList(Endpoint, Endpoint);

        List<String> EndpointIds = Arrays.asList("123");

        Mockito.when(EndpointService.moveEndpoints(Mockito.any(), Mockito.anyString(), Mockito.anyString()))
                .thenReturn(EndpointList);

        RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/endpoint/group/move")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(EndpointIds)).param("from", "gorupA").param("to", "groupB");

        MvcResult result = mvc.perform(requestBuilder).andReturn();

        result = mvc.perform(MockMvcRequestBuilders.asyncDispatch(result)).andDo(MockMvcResultHandlers.print()).andReturn();


        JsonNode actual = objectMapper.readTree(result.getResponse().getContentAsString()).get("content");

        String expected = objectMapper.writeValueAsString(Arrays.asList(EndpointList));

        Assert.assertEquals(expected, actual.toString());

    }

【问题讨论】:

    标签: java spring-mvc junit mockito spring-boot-test


    【解决方案1】:

    1) 你有带有 url 的控制器

    @PostMapping("/endpoint/group/move")
    

    但在测试中,您向“/Endpoints/group/move”(附加)发送请求

    post("/Endpoints/group/move")
    

    2) 而且你不需要 '.get("content")' 因为你已经解析了响应体。看起来您不需要解析 JsonNode 的答案,只需使用字符串

    String actual = result.getResponse().getContentAsString();
    String expected = objectMapper.writeValueAsString(Arrays.asList(EndpointList));
    Assert.assertEquals(expected, actual.toString());
    

    【讨论】:

    • 运行测试时出现以下错误“在指定的 timeToWait=10000 期间未设置处理程序的异步结果”
    • 您是否在测试期间调试了代码并检查结果是否真的在控制器中设置为DefferedResult? ComplitableFuture 使用什么执行器?
    • 你能解释一下你在问什么
    • 我的意思是你确定result.setResult(ResponseEntity.status(HttpStatus.OK).body(movedEndpointsList));result.setResult(ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(movedEndpointsList)); 之一真的被执行了吗?根据您的错误消息,测试中的控制器似乎没有响应。我编写了相同的控制器和测试,并且只有当 CompletableFuture 的异常被抛出并且throw new RuntimeException(ex); 被执行时才会有相同的消息。如果你能分享完整的代码,那将是非常有帮助的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    相关资源
    最近更新 更多