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


Java MockMvcRequestBuilders類代碼示例

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


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

示例1: should_get_the_preset_shop_list

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void should_get_the_preset_shop_list() throws Exception {
    MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/api/shops")
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andReturn();
    List<ShopResponse> shops = this.objectMapper.readValue(
            mvcResult.getResponse().getContentAsByteArray(),
            new TypeReference<List<ShopResponse>>(){});
    assertEquals(1, shops.size());

    ShopResponse shop = shops.get(0);
    assertEquals(shopName, shop.getName());


}
 
開發者ID:DDD-MicroService-Kata,項目名稱:petstore-inventory,代碼行數:17,代碼來源:ShopApiTest.java

示例2: testAddBook_Form_validation

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void testAddBook_Form_validation() {
	try {

		mockMVC.perform(MockMvcRequestBuilders.post("/addBook.htm")
				
				.contentType(MediaType.APPLICATION_FORM_URLENCODED)
				.param("bookName", "Book_test")
				.param("author", "author_test")
				.param("description", "adding book for test")
				.param("ISBN", "1234")
				.param("price", "9191")
				.param("publication", "This is the test publication")
				.requestAttr("book", new Book()))
				.andExpect(MockMvcResultMatchers.view().name("bookForm"))
				.andExpect(
						MockMvcResultMatchers
								.model()
								.attributeHasErrors("book"))
				
				.andDo(MockMvcResultHandlers.print());
	} catch (Exception e) {
		// TODO: handle exception
		fail(e.getMessage());
		e.printStackTrace();
	}
}
 
開發者ID:PacktPublishing,項目名稱:Learning-Spring-5.0,代碼行數:28,代碼來源:TestAddBookController.java

示例3: testProcessSingleRadioMapFile

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void testProcessSingleRadioMapFile() {

    try {
        long buildingId = TestHelper.addNewBuildingAndRetrieveId(this.mockMvc, this.contentType);
        assertTrue("Failed to add new building.", buildingId > 0);

        mockMvc.perform(MockMvcRequestBuilders.fileUpload("/position/processRadioMapFiles")
                .file(radioMapFileR01A5)
                .param("buildingIdentifier", String.valueOf(buildingId)))
                .andExpect(status().isOk());


    } catch (Exception e) {
        e.printStackTrace();
        fail("An unexpected Exception of type " + e.getClass().getSimpleName() + " has occurred.");
    }


}
 
開發者ID:ProjectIndoor,項目名稱:projectindoorweb,代碼行數:21,代碼來源:EverythingControllerTest.java

示例4: testProcessEmptyRadioMapFile

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void testProcessEmptyRadioMapFile() {

    try {
        long buildingId = TestHelper.addNewBuildingAndRetrieveId(this.mockMvc, this.contentType);
        assertTrue("Failed to add new building.", buildingId > 0);

        mockMvc.perform(MockMvcRequestBuilders.fileUpload("/position/processRadioMapFiles")
                .file(emptyRadioMapFileHfT)
                .file(transformedPointsFile)
                .param("buildingIdentifier", String.valueOf(buildingId)))
                .andExpect(status().isOk());


    } catch (Exception e) {
        e.printStackTrace();
        fail("An unexpected Exception of type " + e.getClass().getSimpleName() + " has occurred.");
    }


}
 
開發者ID:ProjectIndoor,項目名稱:projectindoorweb,代碼行數:22,代碼來源:EverythingControllerTest.java

示例5: processEvaluationFileForBuilding

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
private long processEvaluationFileForBuilding(long buildingId) throws Exception {

        mockMvc.perform(MockMvcRequestBuilders.fileUpload("/position/processEvalFiles")
                .file(evaluationFile)
                .param("buildingIdentifier", String.valueOf(buildingId)))
                .andExpect(status().isOk());

        ResultActions getEvalFileResultActions = mockMvc.perform(get("/position/getEvalFilesForBuildingId?" +
                "buildingIdentifier=" + buildingId));
        getEvalFileResultActions.andExpect(status().isOk());
        String getEvalFileResult = getEvalFileResultActions.andReturn().getResponse().getContentAsString();
        List<GetEvaluationFilesForBuilding> getEvaluationFilesForBuilding = (List<GetEvaluationFilesForBuilding>)
                this.objectMapper.readValue(getEvalFileResult, new TypeReference<List<GetEvaluationFilesForBuilding>>() {
                });
        assertTrue("The returned list of type " + GetEvaluationFilesForBuilding.class.getSimpleName() + " had an unexpected size.",
                getEvaluationFilesForBuilding.size() == 1);

        return getEvaluationFilesForBuilding.get(0).getId();

    }
 
開發者ID:ProjectIndoor,項目名稱:projectindoorweb,代碼行數:21,代碼來源:EverythingControllerTest.java

示例6: testUpdatePassword

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void testUpdatePassword() throws Exception {
    User user = UserFixture.buildPersistentUser();

    when(userRepository.findOne(2L))
            .thenReturn(user);

    mvc.perform(MockMvcRequestBuilders.put("/user/{userId}/change-password", 2L).content(PASSWORD_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)).andExpect(status().isAccepted());

    user.setPassword("ZYX");

    verify(userRepository, times(1))
            .save(user);
}
 
開發者ID:crowdcode-de,項目名稱:spring-cloud-performance-tuning,代碼行數:17,代碼來源:UserControllerTest.java

示例7: testPage

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void testPage() throws Exception{
	//模擬請求拿到返回值
	
	MvcResult result = movkMVC.perform(MockMvcRequestBuilders.get("/emps").param("pn", "3")).andReturn();
	//請求成功以後,請求域中會有pageInfo:我們可以取出pageinfo進行驗證
	MockHttpServletRequest request = result.getRequest();	
	PageInfo pi = (PageInfo)request.getAttribute("PageInfo");
	System.out.println("當前頁碼:"+pi.getPageNum());
	System.out.println("總頁碼:"+pi.getPages());
	System.out.println("總記錄數:"+pi.getTotal());
	System.out.println("在頁麵需要連續顯示的頁碼");
	int[]  nums= pi.getNavigatepageNums();
	for(int i:nums){
		System.out.println(" "+i);
	}
	
	//獲取員工數據
	List<Employee> list= pi.getList();
	for(Employee employee : list ){
		System.out.println("ID:" +employee.getEmpId()+"==>Name:"+employee.getEmpName());
	}
	
}
 
開發者ID:xpppp111,項目名稱:SSM-,代碼行數:25,代碼來源:MvcTest.java

示例8: test

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void test() throws Exception
{
    mockUtil.requestContextExecutor().execute(new Executable()
    {
        @Override
        public void execute() throws Exception
        {
            mvc.perform(MockMvcRequestBuilders.get("/hello"))
                .andExpect(status().isOk())
                .andExpect(content().json(
                    IOUtils.toString(
                        currentThread().getContextClassLoader().getResourceAsStream("expected.json"))));
        }
    });
}
 
開發者ID:SAP,項目名稱:cloud-s4-sdk-examples,代碼行數:17,代碼來源:HelloWorldServiceTest.java

示例9: testHttpGet

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void testHttpGet() throws Exception {
    mockSdk.requestContextExecutor().execute(new Executable() {
        @Override
        public void execute() throws Exception {
            ResultActions action = mockMvc.perform(MockMvcRequestBuilders
                    .put("/callback/tenant/" + TENANT_ID_1));
            action.andExpect(MockMvcResultMatchers.status().is2xxSuccessful());

            action = mockMvc.perform(MockMvcRequestBuilders
                    .get("/cost-center"));
            action.andExpect(MockMvcResultMatchers.status().isOk());

            action = mockMvc.perform(MockMvcRequestBuilders
                    .delete("/callback/tenant/" + TENANT_ID_1));
            action.andExpect(MockMvcResultMatchers.status().is2xxSuccessful());
        }
    });
}
 
開發者ID:SAP,項目名稱:cloud-s4-sdk-examples,代碼行數:20,代碼來源:CostCenterServiceIntegrationTest.java

示例10: testHttpPost

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void testHttpPost() throws Exception {
    final String newCostCenterJson = buildCostCenterJson(COSTCENTER_ID_1);
    mockSdk.requestContextExecutor().execute(new Executable() {
        @Override
        public void execute() throws Exception {
            ResultActions action = mockMvc.perform(MockMvcRequestBuilders
                    .put("/callback/tenant/" + TENANT_ID_1));
            action.andExpect(MockMvcResultMatchers.status().is2xxSuccessful());

            action = mockMvc
                    .perform(MockMvcRequestBuilders
                            .request(HttpMethod.POST, "/cost-center")
                            .contentType(MediaType.APPLICATION_JSON)
                            .accept(MediaType.APPLICATION_JSON)
                            .content(newCostCenterJson));
            action.andExpect(MockMvcResultMatchers.status().isOk());

            action = mockMvc.perform(MockMvcRequestBuilders
                    .delete("/callback/tenant/" + TENANT_ID_1));
            action.andExpect(MockMvcResultMatchers.status().is2xxSuccessful());
        }
    });
}
 
開發者ID:SAP,項目名稱:cloud-s4-sdk-examples,代碼行數:25,代碼來源:CostCenterServiceIntegrationTest.java

示例11: testAddBook

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void testAddBook() {
	try {
		mockMVC.perform(MockMvcRequestBuilders.post("/addBook.htm")
				
						.contentType(MediaType.APPLICATION_FORM_URLENCODED)
						.param("bookName", "Book_test")
						.param("author", "author_test")
						.param("description", "adding book for test")
						.param("ISBN", "1234")
						.param("price", "9191")
						.param("publication", "This is the test publication")
						.requestAttr("book", new Book()))
				.andExpect(MockMvcResultMatchers.view().name("display"))
				.andExpect(MockMvcResultMatchers.model().attribute("auth_name","author_test"))
				.andDo(MockMvcResultHandlers.print());
		;
	} catch (Exception e) {
		// TODO: handle exception
		fail(e.getMessage());

	}

}
 
開發者ID:PacktPublishing,項目名稱:Learning-Spring-5.0,代碼行數:25,代碼來源:TestAddBookController.java

示例12: checkApplicationInfoTest

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void checkApplicationInfoTest() throws Exception {
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/info").contentType(MediaType.ALL)
            .accept(MediaType.ALL);

    mvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk());
}
 
開發者ID:kenly333,項目名稱:service-hive,代碼行數:8,代碼來源:HRClientTests.java

示例13: getCustomerByIdShouldReturnCustomer

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void getCustomerByIdShouldReturnCustomer() throws Exception {

    Mockito
            .when(this.customerRepository.findOne(1L))
            .thenReturn(c1);

    mockMvc.perform(MockMvcRequestBuilders.get("/customers/1"))
            .andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
            .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(MockMvcResultMatchers.jsonPath("@.id").value(1L))
            .andDo(MockMvcRestDocumentation.document("customerById"));
}
 
開發者ID:applied-continuous-delivery-livelessons,項目名稱:cdct,代碼行數:14,代碼來源:CustomerServiceRestdocsApplicationTests.java

示例14: getCustomersShouldReturnAllCustomers

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void getCustomersShouldReturnAllCustomers() throws Exception {

    Mockito
            .when(this.customerRepository.findAll())
            .thenReturn(Arrays.asList(c1, c2));

    mockMvc.perform(MockMvcRequestBuilders.get("/customers"))
            .andExpect(MockMvcResultMatchers.jsonPath("@.[0].id").value(1L))
            .andExpect(MockMvcResultMatchers.jsonPath("@.[0].first").value("first"))
            .andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
            .andDo(document("customers"));
}
 
開發者ID:applied-continuous-delivery-livelessons,項目名稱:cdct,代碼行數:14,代碼來源:CustomerServiceRestdocsApplicationTests.java

示例15: customersShouldReturnAllCustomers

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; //導入依賴的package包/類
@Test
public void customersShouldReturnAllCustomers() throws Exception {

    Mockito.when(this.customerRepository.findAll()).thenReturn(Arrays.asList(
            new Customer(1L, "first", "last", "[email protected]"),
            new Customer(2L, "first", "last", "[email protected]")));

    this.mockMvc.perform(MockMvcRequestBuilders.get("/customers"))
            .andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
            .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(MockMvcResultMatchers.jsonPath("@.[0].id").value(1L))
            .andExpect(MockMvcResultMatchers.jsonPath("@.[0].firstName").value("first"))
            .andExpect(MockMvcResultMatchers.jsonPath("@.[1].id").value(2L))
            .andDo(MockMvcRestDocumentation.document("customers"));
}
 
開發者ID:applied-continuous-delivery-livelessons,項目名稱:cdct,代碼行數:16,代碼來源:CustomerRestControllerTest.java


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