一起测试Spring asyncResult()和jsonPath()

时间:2013-07-17 22:49:27

标签: json spring-mvc spring-test spring-test-mvc

我正在使用一个安静的网址启动一个长期运行的后端进程(通常是在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 = []

3 个答案:

答案 0 :(得分:17)

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-library jar。

对于maven ......

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>LATEST VERSION HERE</version>
        <scope>test</scope>
    </dependency>

答案 1 :(得分:8)

马特的回答是正确的,但我希望perform能够正常运作。 下面是一个执行方法,可用于测试异步和同步请求。因此,您不需要在测试中关注后端如何处理请求。你只对实际的反应感兴趣,对吗?

ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
    ResultActions resultActions = mockMvc.perform(builder);
    if (resultActions.andReturn().getRequest().isAsyncStarted()) {
      return mockMvc.perform(asyncDispatch(resultActions
          .andExpect(request().asyncResult(anything()))
          .andReturn()));
    } else {
      return resultActions;
    }
}

将它集成到您​​的测试中的一种方法是将它放在一个公共的抽象基类中,并从中扩展您的实际测试类:

import static org.hamcrest.Matchers.anything;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml")
public abstract class AbstractMockMvcTests {

  @Autowired
  protected WebApplicationContext wac;

  private MockMvc mockMvc;

  @Before
  public void setup() throws Exception {
    mockMvc = webAppContextSetup(this.wac).build();
  }

  protected ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
    ResultActions resultActions = mockMvc.perform(builder);
    if (resultActions.andReturn().getRequest().isAsyncStarted()) {
      return mockMvc.perform(asyncDispatch(resultActions
          .andExpect(request().asyncResult(anything()))
          .andReturn()));
    } else {
      return resultActions;
    }
  }
}

然后通过扩展基类并使用perform方法来实现测试。 在这个例子中,mockMvc是私有的,可以温和地指导所有未来的测试作者 使用自定义执行方法。

@RunWith(SpringJUnit4ClassRunner.class)
public class CallableControllerTests extends AbstractMockMvcTests {

  @Test
  public void responseBodyAsync() throws Exception {
    perform(get("/async/callable/response-body"))
      .andExpect(status().isOk())
      .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
      .andExpect(content().string("Callable result"));
  }

  @Test
  public void responseBodySync() throws Exception {
    perform(get("/sync/foobar/response-body"))
      .andExpect(status().isOk())
      .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
      .andExpect(content().string("Sync result"));
  }
}

答案 2 :(得分:5)

我认为您希望在启动的异步调用的结果上使用asyncDispatch 来自以下链接的参考代码

http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.html

用法涉及首先执行一个启动异步处理的请求:

 MvcResult mvcResult = this.mockMvc.perform(get("/trigger/job/xyz"))
        .andExpect(request().asyncStarted())
        .andReturn();

然后执行异步调度重新使用MvcResult:

 this.mockMvc.perform(asyncDispatch(mvcResult))
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
        .andExpect(content().string(.......));

或在你的情况下

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"));