Bud的回答确实帮助我指出了正确的方向,但是由于它没有等待异步结果,因此并没有奏效。自发布此问题以来,spring-mvc-
showcase示例(https://github.com/SpringSource/spring-mvc-
showcase)已更新。
看起来在调用的第一部分中,当您检索MvcResult时,您需要在asyncResult()上声明,对于JSON
pojo映射,您需要在实际的类型本身(而不是JSON)上声明。所以我需要在Bud的答案中添加第三行,然后其余的就可以了。
MvcResult mvcResult = this.mockMvc.perform(get("/trigger/job/xyz")) .andExpect(request().asyncStarted()) .andExpect(request().asyncResult(instanceOf(TriggerResult.class))) .andReturn();this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("status").value("SUCCESS")) .andExpect(jsonPath("message").value("A meaningful message appears"));注意:
instanceOf()是
org.hamcrest.CoreMatchers.instanceOf。要访问Hamcrest库,请包含最新的
hamcrest-libraryjar。
对于行家…
<dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>LATEST VERSION HERE</version> <scope>test</scope> </dependency>



