java - DeferredResult 的 Spring MVC 单元测试不调用超时回调

标签 java spring-mvc spring-test-mvc

我在 Java 7 上使用 Spring 4.3.18 和 Spring Boot 1.5.14。

我正在实现一个 RestController 端点,它返回一个带有超时回调的 DeferredResult 。我正在尝试为超时回调编写单元测试,但无法获得 MockMvc 单元测试来调用超时回调。

为了测试,我编写了这个端点:

@PostMapping("/test")
public DeferredResult<String>
testit() {
    logger.info("testit called");
    final DeferredResult<String> rv = new DeferredResult<>(1000L);
    rv.onTimeout(new Runnable() {
        @Override
        public void run() {
            logger.info("run called");
            rv.setResult("timed out");
        }
    });
    return rv;
}

这个单元测试:

@Autowired
private MockMvc mockMvc;

@Test
public void testTest() throws Exception {
    MvcResult result = mockMvc.perform(post("/rest/tasks/test"))
        .andExpect(request().asyncStarted())
        .andReturn();
    result.getAsyncResult(1500);
    mockMvc.perform(asyncDispatch(result))
        .andExpect(status().isOk())
        ;
}

(对 result.getAsyncResult(1500) 的调用基于 https://jira.spring.io/browse/SPR-16869 )

当我运行此命令时,将调用 testit() 端点,在 1500 毫秒延迟后,我收到一个异常,提示 setResult() 从未被调用。未调用超时处理程序:

java.lang.IllegalStateException: Async result for handler [public org.springframework.web.context.request.async.DeferredResult<java.lang.String> my.package.TasksController.testit()] was not set during the specified timeToWait=1500
    at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:135)
    at my.package.TasksControllerTest.testTest(TasksControllerTest.java:200)
2517 [main] INFO my.package.TasksController - testit called

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /rest/tasks/test
       Parameters = {}
          Headers = {}

Handler:
             Type = my.package.TasksController
           Method = public org.springframework.web.context.request.async.DeferredResult<java.lang.String> my.package.TasksController.testit()

Async:
    Async started = true
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []
4037 [Thread-2] INFO org.springframework.web.context.support.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@6ea91f6: startup date [Thu Jul 26 19:44:31 EDT 2018]; root of context hierarchy

我需要做什么才能使测试框架调用 DeferredResult 上的超时处理程序?

最佳答案

似乎可以在单元测试中创建一个合成超时,如下所示:

@Test
public void testTest() throws Exception {
    MvcResult result = mockMvc.perform(post("/rest/tasks/test"))
        .andExpect(request().asyncStarted())
        .andReturn();
    // Trigger a timeout on the request
    MockAsyncContext ctx = (MockAsyncContext) result.getRequest().getAsyncContext();
    for (AsyncListener listener : ctx.getListeners()) {
        listener.onTimeout(null);
    }
    mockMvc.perform(asyncDispatch(result))
        .andExpect(status().isOk())
        ;
}

访问请求的异步监听器并调用 onTimeout() 将导致调用 DeferredRequest 的超时回调。

我的问题中对 result.getAsyncResult(1500) 的调用对于此测试来说是多余的,因为 asyncDispatch() 将调用 getAsyncResult() > 无论如何。

关于java - DeferredResult 的 Spring MVC 单元测试不调用超时回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51549043/

相关文章:

Javafx:ListView 禁用列表的指定元素

java - 通过数组列表搜索? java

java - 使用 spring 3 restful 以编程方式更改 http 响应状态

Spring Annotation @WebMvcTest 在具有 Jpa 存储库的应用程序中不起作用

spring - 是否可以在没有 SpringJUnit4ClassRunner 的情况下使用 MockMvc?

java - 使用按功能包约定将我的框架类放在哪里?

java - Jedis SpringBoot 无法序列化和反序列化 Map<String, Object>

java - 使用 JSTL c :out ${expression} 动态评估表达式

java - Spring MVC - 在 Controller 中默认设置值

spring - 使用 MockMVC 测试 Spring 4.0.3 Controller (MVC)