我正在使用一个安静的 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 = []