本文整理汇总了Java中org.springframework.test.web.servlet.result.MockMvcResultMatchers类的典型用法代码示例。如果您正苦于以下问题:Java MockMvcResultMatchers类的具体用法?Java MockMvcResultMatchers怎么用?Java MockMvcResultMatchers使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MockMvcResultMatchers类属于org.springframework.test.web.servlet.result包,在下文中一共展示了MockMvcResultMatchers类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetUsersPage0Size20
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void testGetUsersPage0Size20() throws Exception {
final MvcResult mvcResult = mockMvc
.perform(get(UserController.REQUEST_PATH_API_USERS).accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.request().asyncStarted())
.andReturn();
mockMvc
.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.content", Matchers.hasSize(2)))
.andExpect(jsonPath("$.content[0].email").value("[email protected]"))
.andExpect(jsonPath("$.content[0].firstName").value("User"))
.andExpect(jsonPath("$.content[0].lastName").value("One"))
.andExpect(jsonPath("$.content[1].email").value("[email protected]"))
.andExpect(jsonPath("$.content[1].firstName").value("User"))
.andExpect(jsonPath("$.content[1].lastName").value("Two"))
.andExpect(jsonPath("$.totalElements").value(2))
.andExpect(jsonPath("$.totalPages").value(1))
.andExpect(jsonPath("$.size").value(20))
.andExpect(jsonPath("$.number").value(0))
.andExpect(jsonPath("$.first").value("true"))
.andExpect(jsonPath("$.last").value("true"));
}
示例2: siteSettingsUpdated_UpdatesSiteOptions
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
@WithAdminUser
public void siteSettingsUpdated_UpdatesSiteOptions() throws Exception {
RequestBuilder request = post("/admin/site/settings")
.param(ISiteOption.SITE_NAME, siteOptionMapDTO.getSiteName())
.param(ISiteOption.SITE_DESCRIPTION, siteOptionMapDTO.getSiteDescription())
.param(ISiteOption.ADD_GOOGLE_ANALYTICS, String.valueOf(siteOptionMapDTO.getAddGoogleAnalytics()))
.param(ISiteOption.GOOGLE_ANALYTICS_TRACKING_ID, siteOptionMapDTO.getGoogleAnalyticsTrackingId())
.param(ISiteOption.USER_REGISTRATION, String.valueOf(siteOptionMapDTO.getUserRegistration()))
.with(csrf());
mvc.perform(request)
.andExpect(model().attributeHasNoErrors())
.andExpect(MockMvcResultMatchers.flash().attributeExists("feedbackMessage"))
.andExpect(redirectedUrl("/admin/site/settings"));
}
示例3: testHttpAddSuccess
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void testHttpAddSuccess() throws Exception {
final SapClient sapClient = mockSdk.getErpSystem().getSapClient();
final String newCostCenterJson = getNewCostCenterAsJson(DEMO_COSTCENTER_ID);
final RequestBuilder newCostCenterRequest = MockMvcRequestBuilders
.request(HttpMethod.POST, "/api/v1/rest/client/"+sapClient+"/controllingarea/"+DEMO_CONTROLLING_AREA+"/costcenters")
.param("testRun", "true")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(newCostCenterJson);
mockSdk.requestContextExecutor().execute(new Executable() {
@Override
public void execute() throws Exception {
mockMvc.perform(newCostCenterRequest).andExpect(MockMvcResultMatchers.status().isOk());
}
});
}
示例4: findAllVendorsTest
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void findAllVendorsTest(){
List<String> vendorNames = new ArrayList<String>();
for(VendorEntity vendorEntity : vendorEntityTestRig.getVendorEntitiesMap().values()){
vendorNames.add(vendorEntity.getVendorName());
}
try {
//http://localhost:8080/vendors/search/getVendorByVendorNameContainsIgnoreCase?vendorName=red%20hat
mockMvc.perform(get("/vendors"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("_embedded.vendors",hasSize(vendorEntityTestRig.getVendorEntitiesMap().size())))
.andExpect(MockMvcResultMatchers.jsonPath("_embedded.vendors[*].vendorName", containsInAnyOrder(vendorNames.toArray())))
.andDo(print());
} catch (Exception e) {
e.printStackTrace();
}
}
示例5: updatePostWithValidData_RedirectsToPermalinkPage
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void updatePostWithValidData_RedirectsToPermalinkPage() throws Exception {
String newTitle = "New Title for updatePostWithValidData_RedirectsToPermalinkPage Test";
Post post = postService.getPostById(1L);
RequestBuilder request = post("/admin/posts/update")
.param("postId", "1")
.param("displayType", String.valueOf(post.getDisplayType()))
.param("postContent", post.getPostContent())
.param("twitterCardType", post.getPostMeta().getTwitterCardType().name())
.param("postTitle", newTitle)
.param("tags", "updatePostWithValidData1, updatePostWithValidData2")
.with(csrf());
mvc.perform(request)
.andExpect(model().hasNoErrors())
.andExpect(MockMvcResultMatchers.flash().attributeExists("feedbackMessage"))
.andExpect(redirectedUrl("/admin/posts"));
Post updatedPost = postService.getPostById(1L);
assert (updatedPost.getPostTitle().equals(newTitle));
}
示例6: testGetUsersPage0Size1
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void testGetUsersPage0Size1() throws Exception {
final MockHttpServletRequestBuilder getRequest = get(UserController.REQUEST_PATH_API_USERS)
.param("size", "1")
.accept(MediaType.APPLICATION_JSON);
final MvcResult mvcResult = mockMvc
.perform(getRequest)
.andExpect(MockMvcResultMatchers.request().asyncStarted())
.andReturn();
mockMvc
.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.content", Matchers.hasSize(1)))
.andExpect(jsonPath("$.content[0].email").value("[email protected]"))
.andExpect(jsonPath("$.totalElements").value(2))
.andExpect(jsonPath("$.totalPages").value(2))
.andExpect(jsonPath("$.size").value(1))
.andExpect(jsonPath("$.number").value(0))
.andExpect(jsonPath("$.first").value("true"))
.andExpect(jsonPath("$.last").value("false"));
}
示例7: testGetUsersPage1Size1
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void testGetUsersPage1Size1() throws Exception {
final MockHttpServletRequestBuilder getRequest = get("/api/users")
.param("page", "1")
.param("size", "1")
.accept(MediaType.APPLICATION_JSON);
final MvcResult mvcResult = mockMvc
.perform(getRequest)
.andExpect(MockMvcResultMatchers.request().asyncStarted())
.andReturn();
mockMvc
.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.content", Matchers.hasSize(1)))
.andExpect(jsonPath("$.content[0].email").value("[email protected]"))
.andExpect(jsonPath("$.totalElements").value(2))
.andExpect(jsonPath("$.totalPages").value(2))
.andExpect(jsonPath("$.size").value(1))
.andExpect(jsonPath("$.number").value(1))
.andExpect(jsonPath("$.first").value("false"))
.andExpect(jsonPath("$.last").value("true"));
}
示例8: testGetUser
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void testGetUser() throws Exception {
final MockHttpServletRequestBuilder getRequest = get(UserController.REQUEST_PATH_API_USERS +
"/590f86d92449343841cc2c3f")
.accept(MediaType.APPLICATION_JSON);
final MvcResult mvcResult = mockMvc
.perform(getRequest)
.andExpect(MockMvcResultMatchers.request().asyncStarted())
.andReturn();
mockMvc
.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.lastName").value("One"));
}
示例9: testHttpGet
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的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());
}
});
}
示例10: testHttpPost
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的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());
}
});
}
示例11: testHttpAddFailure
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void testHttpAddFailure() throws Exception {
final SapClient sapClient = mockSdk.getErpSystem().getSapClient();
final String newCostCenterJson = getNewCostCenterAsJson(EXISTING_COSTCENTER_ID);
// {"costcenter":"012346578","description":"demo","validFrom":...}
// cost center already exists in database
mockSdk.requestContextExecutor().execute(new Executable() {
@Override
public void execute() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders
.request(HttpMethod.POST, "/api/v1/rest/client/"+sapClient+"/controllingarea/"+DEMO_CONTROLLING_AREA+"/costcenters")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(newCostCenterJson))
.andExpect(MockMvcResultMatchers
.status()
.is5xxServerError());
}
});
}
示例12: getItemAtALocationShouldReturnAllItemsAtThisLocation
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void getItemAtALocationShouldReturnAllItemsAtThisLocation() throws Exception {
String expectedItemDescription = "Teller";
given(this.itemService.getItemsByLocation("Regal A"))
.willReturn(Arrays.asList(new Item(1l,"Teller", "Regal A", LocalDate.now())));
// Testen von JsonPath
// http://jsonpath.com
this.mvc.perform(get("/item/Regal A")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].description").value(expectedItemDescription));
}
示例13: getItemsAtALocationShouldReturnAllItemsAtThisLocation
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void getItemsAtALocationShouldReturnAllItemsAtThisLocation() throws Exception {
String expectedItemDescription1 = "Teller";
String expectedItemDescription2 = "Untertasse";
given(this.itemService.getItemsByLocation("Regal A"))
.willReturn(Arrays.asList(new Item(1l,"Teller", "Regal A", LocalDate.now()),
new Item(1l,"Untertasse", "Regal A", LocalDate.now())));
// Testen von JsonPath
// http://jsonpath.com
this.mvc.perform(get("/item/Regal A").accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].description").value(expectedItemDescription1))
.andExpect(MockMvcResultMatchers.jsonPath("$.[1].description").value(expectedItemDescription2));
}
示例14: updateShouldReturnTheUpdatedItemAnd200
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void updateShouldReturnTheUpdatedItemAnd200() throws Exception {
Item item = new Item(1l,"Teller", "Regal A", null);
given(this.itemService.update(any(Item.class))).willReturn(item);
ObjectMapper mapper = new ObjectMapper();
String req = mapper.writeValueAsString(item);
this.mvc.perform(put("/item", item)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON)
.content(req))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.location").value(item.getLocation()));
}
示例15: createItem
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; //导入依赖的package包/类
@Test
public void createItem() throws Exception {
Item item = new Item(1l,"Teller", "Regal A", null);
ObjectMapper mapper = new ObjectMapper();
String req = mapper.writeValueAsString(item);
String expectedItemDescription = "Teller";
when(itemService.create(any(Item.class))).thenReturn(item);
this.mvc.perform(post("/item", item)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(req))
.andExpect(status().isCreated())
.andExpect(MockMvcResultMatchers.jsonPath("$.description").value(expectedItemDescription));
}