【发布时间】:2020-02-28 20:14:14
【问题描述】:
有没有办法获取 mockMvc 在执行时将使用的实际请求对象: mockMvc.perform(RequestBuilder requestBuilder)
我知道我可以自己构建请求(即)
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() 实现,它只是比较对象 ID。即
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);
【问题讨论】:
标签: spring junit controller easymock mockmvc