Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 如何测试DeferredResult timeoutResult_Spring Mvc_Spring Mvc Test_Spring Web - Fatal编程技术网

Spring mvc 如何测试DeferredResult timeoutResult

Spring mvc 如何测试DeferredResult timeoutResult,spring-mvc,spring-mvc-test,spring-web,Spring Mvc,Spring Mvc Test,Spring Web,我正在实施 在这里,我的转换方法具有与以前相同的响应签名,但它现在使用长轮询,而不是立即响应: private Map<String, DeferredResult<ResponseEntity<?>>> requests = new ConcurrentHashMap<>(); @RequestMapping(value = "/{uuid}", method = RequestMethod.GET) public DeferredResult

我正在实施

在这里,我的转换方法具有与以前相同的响应签名,但它现在使用长轮询,而不是立即响应:

private Map<String, DeferredResult<ResponseEntity<?>>> requests = new ConcurrentHashMap<>();

@RequestMapping(value = "/{uuid}", method = RequestMethod.GET)
public DeferredResult<ResponseEntity<?>> poll(@PathVariable("uuid") final String uuid) {
    // Create & store a new instance
    ResponseEntity<?> pendingOnTimeout = ResponseEntity.accepted().build();
    DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(TWENTYFIVE_SECONDS, pendingOnTimeout);
    requests.put(uuid, deferredResult);

    // Clean up poll requests when done
    deferredResult.onCompletion(() -> {
        requests.remove(deferredResult);
    });

    // Set result if already available
    Task task = taskHolder.retrieve(uuid);
    if (task == null)
        deferredResult.setResult(ResponseEntity.status(HttpStatus.GONE).build());
    else
        // Done (or canceled): Redirect to retrieve file contents
        if (task.getFutureFile().isDone())
            deferredResult.setResult(ResponseEntity.created(RetrieveController.uri(uuid)).build());

    // Return result
    return deferredResult;
}
我得到以下跟踪:

java.lang.IllegalStateException:未在指定的时间内设置处理程序[public org.springframework.web.context.request.Async.DeferredResult>nl.bioprodict.blast.api.PollController.poll(java.lang.String)]的异步结果wait=25000 位于org.springframework.util.Assert.state(Assert.java:392) 位于org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:143) 位于org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:120) 位于org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch(MockMvcRequestBuilders.java:235) 位于nl.bioprodict.blast.docs.PollControllerDocumentation.pollPending(PollControllerDocumentation.java:53)

与此相关的Spring框架测试,我发现所有这些测试似乎都在使用模拟:


我如何测试延迟结果timeoutResult的正确处理?

在我的例子中,在阅读spring源代码并设置超时(10000毫秒)并获得异步结果后,为我解决了它,如下所示

 mvcResult.getRequest().getAsyncContext().setTimeout(10000);
 mvcResult.getAsyncResult();
我的整个测试代码是

MvcResult mvcResult = this.mockMvc.perform(
                                post("<SOME_RELATIVE_URL>")
                                .contentType(MediaType.APPLICATION_JSON)
                                .content(<JSON_DATA>))
                        ***.andExpect(request().asyncStarted())***
                            .andReturn();

***mvcResult.getRequest().getAsyncContext().setTimeout(10000);***
***mvcResult.getAsyncResult();***

this.mockMvc
    .perform(asyncDispatch(mvcResult))
    .andDo(print())
    .andExpect(status().isOk());
MvcResult MvcResult=this.mockMvc.perform(
职位(“”)
.contentType(MediaType.APPLICATION_JSON)
.content())
***.andExpect(请求().asyncStarted())***
.andReturn();
***mvcResult.getRequest().getAsyncContext().setTimeout(10000)***
***mvcResult.getAsyncResult()***
这个.mvc
.perform(异步调度(mvcResult))
.andDo(print())
.andExpect(status().isOk());
希望它能帮助..

I使用Spring 4.3,并设法找到一种方法从单元测试中触发超时回调。获取
MvcResult
后,在调用
asyncDispatch()
之前,可以插入如下代码:

@Test
public void pollPending() throws Exception {
    MvcResult result = mockMvc.perform(get("/poll/{uuid}", uuidPending)).andReturn();
    mockMvc.perform(asyncDispatch(result))
            .andExpect(status().isAccepted());
}
MockAsyncContext ctx = (MockAsyncContext) mvcResult.getRequest().getAsyncContext();
for (AsyncListener listener : ctx.getListeners()) {
    listener.onTimeout(null);
}
请求的一个异步侦听器将调用
DeferredResult
的超时回调

因此,您的单元测试如下所示:

@Test
public void pollPending() throws Exception {
    MvcResult result = mockMvc.perform(get("/poll/{uuid}", uuidPending)).andReturn();
    MockAsyncContext ctx = (MockAsyncContext) result.getRequest().getAsyncContext();
    for (AsyncListener listener : ctx.getListeners()) {
        listener.onTimeout(null);
    }
    mockMvc.perform(asyncDispatch(result))
            .andExpect(status().isAccepted());
}

需要明确的是:它在集成测试中似乎工作得很好,但我还想在
springrestdocs mockmvc
中测试它。我刚刚遇到了完全相同的问题。你们有并没有找到一个解决方案,可以对延迟的结果进行超时测试?@John没有,还并没有,尽管我现在已经不再寻找了。。如果你发现什么,请告诉我@Tim我需要测试同一个案例,你能找到解决方案吗?@Tim,我刚刚收到了相同的错误,原因是
DeferredResult
中的引用为
null
。希望有帮助。谢谢!我觉得它可能会帮助某些人,但在我的情况下,它既不能与
@springbootest()
一起使用,也不能与
@WebMvcTest(PollController.class)
一起使用。两者都继续抛出:
未在指定的时间内设置延迟结果wait=25000
。。谢谢你!这应该是公认的答案,这是唯一适合我的答案