本文整理匯總了Java中org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.param方法的典型用法代碼示例。如果您正苦於以下問題:Java MockHttpServletRequestBuilder.param方法的具體用法?Java MockHttpServletRequestBuilder.param怎麽用?Java MockHttpServletRequestBuilder.param使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder
的用法示例。
在下文中一共展示了MockHttpServletRequestBuilder.param方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testList
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; //導入方法依賴的package包/類
@Test
public void testList() throws Exception {
//創建書架創建的請求
//請求方式為post
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post("/store/list.do");
//有些參數我注釋掉了,你可以自行添加相關參數,得到不同的測試結果
//status為0的記錄
//mockHttpServletRequestBuilder.param("status", "0");
//書架編號為dd的記錄
//mockHttpServletRequestBuilder.param("number", "dd");
//第一頁
mockHttpServletRequestBuilder.param("page", "1");
//每頁10條記錄
mockHttpServletRequestBuilder.param("rows", "10");
mockMvc.perform(mockHttpServletRequestBuilder).andExpect(status().isOk())
.andDo(print());
//控製台會打印如下結果:
//MockHttpServletResponse:
//Status = 200 即為後端成功相應
//返回數據
}
示例2: getHttpResultContent
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; //導入方法依賴的package包/類
public static MvcResult getHttpResultContent(MockMvc mockMvc, String uri, Method method, Map<String, String> keyvals) throws Exception {
MockHttpServletRequestBuilder builder = null;
switch (method) {
case GET:
builder = MockMvcRequestBuilders.get(uri);
break;
case POST:
builder = MockMvcRequestBuilders.post(uri);
break;
case PUT:
builder = MockMvcRequestBuilders.put(uri);
break;
case DELETE:
builder = MockMvcRequestBuilders.delete(uri);
break;
default:
builder = MockMvcRequestBuilders.get(uri);
}
for (Map.Entry<String, String> entry : keyvals.entrySet()) {
builder = builder.param(entry.getKey(), entry.getValue());
}
MvcResult result = mockMvc.perform(builder.accept(MediaType.ALL)).andReturn();
// result.getResponse().getHeaderNames();
return result;
}
示例3: testSave
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; //導入方法依賴的package包/類
@Test
public void testSave() throws Exception {
//創建書架創建的請求
//請求方式為post
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post("/store/save.do");
//添加編號為MockMvc的書架
mockHttpServletRequestBuilder.param("number", "MockMvc");
//書架為兩層
mockHttpServletRequestBuilder.param("level", "2");
mockMvc.perform(mockHttpServletRequestBuilder).andExpect(status().isOk())
.andDo(print());
}
示例4: requestAndPrint
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; //導入方法依賴的package包/類
public static MvcResult requestAndPrint(MockMvc mockMvc, String uri, Method method, RequestParams params) throws Exception {
MockHttpServletRequestBuilder builder = null;
switch (method) {
case GET:
builder = MockMvcRequestBuilders.get(uri);
break;
case POST:
builder = MockMvcRequestBuilders.post(uri);
break;
case PUT:
builder = MockMvcRequestBuilders.put(uri);
break;
case DELETE:
builder = MockMvcRequestBuilders.delete(uri);
break;
default:
builder = MockMvcRequestBuilders.get(uri);
}
for (Map.Entry<String, String> entry : params.entrySet()) {
builder = builder.param(entry.getKey(), entry.getValue());
}
MvcResult result = mockMvc.perform(builder.accept(MediaType.ALL)).andReturn();
Map<String, Object> msg = new LinkedHashMap<>();
Map<String, Object> json = new LinkedHashMap<>();
System.out.println(uri + " " + method.name());
json.put("params", params);
json.put("response_body", "null");
msg.put(method.name(), json);
System.out.println(new Gson().toJson(msg));
System.out.println(result.getResponse().getContentAsString());
return result;
}
示例5: saveProject
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; //導入方法依賴的package包/類
private ProjectVO saveProject(Project project, Long parentId) throws Exception {
ProjectVO projectVO = ProjectConverter.convertTo(project);
MockHttpServletRequestBuilder builder = post(URL_SAVE_PROJECT).content(getObjectMapper()
.writeValueAsString(projectVO)).contentType(EXPECTED_CONTENT_TYPE);
if (parentId != null) {
builder.param("parentId", parentId.toString());
}
// save project
ResultActions actions = mvc()
.perform(builder)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(EXPECTED_CONTENT_TYPE))
.andExpect(MockMvcResultMatchers.jsonPath(JPATH_PAYLOAD).exists())
.andExpect(MockMvcResultMatchers.jsonPath(JPATH_STATUS).value(ResultStatus.OK.name()));
actions.andDo(MockMvcResultHandlers.print());
ResponseResult<ProjectVO> res = getObjectMapper()
.readValue(actions.andReturn().getResponse().getContentAsByteArray(),
getTypeFactory().constructParametrizedType(ResponseResult.class, ResponseResult.class,
ProjectVO.class));
ProjectVO loadedProject = res.getPayload();
Assert.assertNotNull(loadedProject);
return loadedProject;
}