我使用 Restful url 来启动长时间运行的后端进程(它通常按照 cron 计划进行,但我们希望能够手动启动它)。
下面的代码有效,当我手动测试时,我在浏览器中看到结果。
@ResponseBody
@RequestMapping(value = "/trigger/{jobName}", method = RequestMethod.GET)
public Callable<TriggerResult> triggerJob(@PathVariable final String jobName) {
return new Callable<TriggerResult>() {
@Override
public TriggerResult call() throws Exception {
// Code goes here to locate relevant job and kick it off, waiting for result
String message = <result from my job>;
return new TriggerResult(SUCCESS, message);
}
};
}
当我在没有的情况下测试这个时Callable
我使用了下面的代码,一切正常(我更改了预期的错误消息以简化发布)。
mockMvc.perform(get("/trigger/job/xyz"))
.andExpect(status().isOk())
.andDo(print())
.andExpect(jsonPath("status").value("SUCCESS"))
.andExpect(jsonPath("message").value("A meaningful message appears"));
当我添加Callable
但是它不起作用。我也在下面尝试过,但没有成功。还有其他人成功过吗?
mockMvc.perform(get("/trigger/job/xyz"))
.andExpect(status().isOk())
.andDo(print())
.andExpect(request().asyncResult(jsonPath("status").value("SUCCESS")))
.andExpect(request().asyncResult(jsonPath("message").value("A meaningful message appears")));
以下是我的 print() 的相关部分。在这种情况下,mockMvc 似乎无法正确解开 Json(即使它在我的浏览器中工作)?当我这样做时没有Callable
我看到完整的 JSON。
MockHttpServletRequest:
HTTP Method = GET
Request URI = /trigger/job/xyz
Parameters = {}
Headers = {}
Handler:
Type = foo.bar.web.controller.TriggerJobController
Method = public java.util.concurrent.Callable<foo.bar.myproject.web.model.TriggerResult> foo.bar.myproject.web.controller.TriggerJobController.triggerJob(java.lang.String)
Async:
Was async started = true
Async result = foo.bar.myproject.web.model.TriggerResult@67aa1e71
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []