首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MockMvc访问mockMvc将使用的MockHttpServletRequest对象

MockMvc访问mockMvc将使用的MockHttpServletRequest对象
EN

Stack Overflow用户
提问于 2020-02-29 04:14:15
回答 1查看 84关注 0票数 0

有没有办法获得mockMvc在执行时将使用的实际请求对象: mockMvc.perform(RequestBuilder requestBuilder)

我知道我可以自己构建请求(即)

代码语言:javascript
运行
复制
Integer id = new Integer(1);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/myPath").param(Constants.ACTION, Constants.GET_DETAIL)
  .param(Constants.ID, id.toString());
MockHttpServletRequest request = requestBuilder.buildRequest(wac.getServletContext());

但是我不能传递这个请求,因为mockMvc.perform方法只接受将创建MockHttpServletRequest的新实例的构建器。我使用的是EasyMock,它在匹配器中使用equals() (至少在默认情况下是这样),并且由于MockHttpServletRequest中缺少equals()实现,所以它只是比较对象it。i.e

代码语言:javascript
运行
复制
      EasyMock.reset(localeHelper);

  localeHelper.getLocale(request);
  EasyMock.expectLastCall().andReturn(locale);
  /* this matcher will always fail because the request object is rebuilt by the mockMvc.perform(requestBuilder) call 
    and MockHttpServletRequest does not have an equals() method that these mocking tools can fall back on for object equivalency */
  EasyMock.replay(localeHelper);
EN

回答 1

Stack Overflow用户

发布于 2020-03-09 09:14:43

你可能想要抓人。

代码语言:javascript
运行
复制
Capture<HttpServletRequest> capture  = EasyMock.newCapture();
EasyMock.expect(localeHelper.getLocale(EasyMock.capture(capture))).andReturn(locale);
EasyMock.replay(localeHelper);

mockMvc.perform(requestBuilder);

HttpServletRequest request = capture.getValue();
// Then assert whatever you want on the `request` that was received in parameter by the mock
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60458659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档