當前位置: 首頁>>代碼示例>>Java>>正文


Java MockMvcRequestBuilders.post方法代碼示例

本文整理匯總了Java中org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post方法的典型用法代碼示例。如果您正苦於以下問題:Java MockMvcRequestBuilders.post方法的具體用法?Java MockMvcRequestBuilders.post怎麽用?Java MockMvcRequestBuilders.post使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.test.web.servlet.request.MockMvcRequestBuilders的用法示例。


在下文中一共展示了MockMvcRequestBuilders.post方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testList

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入方法依賴的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 即為後端成功相應
    //返回數據
}
 
開發者ID:ZHENFENG13,項目名稱:ssm-demo,代碼行數:23,代碼來源:StoreControllerTest.java

示例2: getHttpResultContent

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入方法依賴的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;
    }
 
開發者ID:aollio,項目名稱:school-express-delivery,代碼行數:27,代碼來源:TestUtil.java

示例3: testSave

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入方法依賴的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());
}
 
開發者ID:ZHENFENG13,項目名稱:ssm-demo,代碼行數:13,代碼來源:StoreControllerTest.java

示例4: testArticleAdd

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入方法依賴的package包/類
@Test
public void testArticleAdd() throws Exception {
    //創建文章對象
    Article article = new Article();
    article.setAddName("13");
    article.setArticleTitle("title");
    article.setArticleContent("content");
    String requestParam = JSONObject.toJSONString(article);
    //請求方式為post
    MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post("/articles");
    mockMvc.perform(mockHttpServletRequestBuilder.contentType(MediaType.APPLICATION_JSON).content(requestParam)).andExpect(status().isOk())
            .andDo(print());

}
 
開發者ID:ZHENFENG13,項目名稱:perfect-ssm,代碼行數:15,代碼來源:ArticleControllerTest.java

示例5: requestAndPrint

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入方法依賴的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;
    }
 
開發者ID:aollio,項目名稱:school-express-delivery,代碼行數:39,代碼來源:TestUtil.java

示例6: testPay

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入方法依賴的package包/類
/**
 * Method: pay(HttpServletRequest request, HttpServletResponse response)
 */
@Test
public void testPay() throws Exception {
    MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post("/union/pay");
    request.contentType(MediaType.APPLICATION_FORM_URLENCODED);

    mockMvc.perform(request)
            .andExpect(status().isOk())
            .andDo(MockMvcResultHandlers.print());
}
 
開發者ID:wangfei0904306,項目名稱:unionpay,代碼行數:13,代碼來源:UnipayControllerTest.java


注:本文中的org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。