本文整理汇总了Java中org.springframework.test.web.servlet.ResultActions类的典型用法代码示例。如果您正苦于以下问题:Java ResultActions类的具体用法?Java ResultActions怎么用?Java ResultActions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ResultActions类属于org.springframework.test.web.servlet包,在下文中一共展示了ResultActions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testFindPrivilegeByName
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testFindPrivilegeByName() throws Exception {
PrivilegeEntity dbResult = new PrivilegeEntity()
.setId("123")
.setName("12345")
.setDescription("Description 12345");
when(privilegeService.findPrivilegeByIdOrName("123")).thenReturn(dbResult);
ResultActions resultActions = mockMvc.perform(get("/api/privileges/123").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("privilege-read"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
verify(privilegeService).findPrivilegeByIdOrName("123");
assertThat(response.getContentAsByteArray())
.isEqualTo(objectMapper.writeValueAsBytes(PrivilegeRestData.builder().fromPrivilegeEntity(dbResult).build()));
}
示例2: http_get_hello_endpoint
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void http_get_hello_endpoint() throws Exception {
logger.info(Boot_Container_Test.TC_HEAD + "simple mvc test" );
// mock does much validation.....
ResultActions resultActions = mockMvc.perform(
get( "/hello" )
.accept( MediaType.TEXT_PLAIN ) );
//
String result = resultActions
.andExpect( status().isOk() )
.andExpect( content().contentType( "text/plain;charset=UTF-8" ) )
.andReturn().getResponse().getContentAsString();
logger.info( "result:\n" + result );
assertThat( result )
.startsWith( "Hello") ;
}
示例3: subjectZoneDoesNotExistException
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void subjectZoneDoesNotExistException() throws Exception {
// NOTE: To throw a ZoneDoesNotExistException, we must ensure that the AcsRequestContext in the
// SpringSecurityZoneResolver class returns a null ZoneEntity
MockSecurityContext.mockSecurityContext(null);
MockAcsRequestContext.mockAcsRequestContext();
BaseSubject subject = JSON_UTILS.deserializeFromFile("controller-test/a-subject.json", BaseSubject.class);
MockMvcContext putContext = TEST_UTILS.createWACWithCustomPUTRequestBuilder(this.wac,
"zoneDoesNotExist", SUBJECT_BASE_URL + '/' + subject.getSubjectIdentifier());
ResultActions resultActions = putContext.getMockMvc().perform(putContext.getBuilder()
.contentType(MediaType.APPLICATION_JSON).content(OBJECT_MAPPER.writeValueAsString(subject)));
resultActions.andExpect(status().isBadRequest());
resultActions.andReturn().getResponse().getContentAsString().contentEquals("Zone not found");
MockSecurityContext.mockSecurityContext(this.testZone);
MockAcsRequestContext.mockAcsRequestContext();
}
示例4: testFindRoles
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testFindRoles() throws Exception {
RoleEntity dbResult = new RoleEntity()
.setId("123")
.setCode("12345")
.setDescription("Description 12345");
Page<RoleEntity> pageResponseBody = new PageImpl<>(Arrays.asList(dbResult));
Page<RoleRestData> expectedResponseBody = new PageImpl<>(Arrays.asList(RoleRestData.builder()
.fromRoleEntity(dbResult).build()));
when(roleService.findRoles(anyString(), any())).thenReturn(pageResponseBody);
ResultActions resultActions = mockMvc.perform(get("/api/roles")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("role-read-all"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
verify(roleService).findRoles(anyString(), any());
assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(expectedResponseBody));
}
示例5: testLoadHistogram
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
private void testLoadHistogram(long fileId) throws Exception {
TrackQuery histogramQuery = initTrackQuery(fileId);
ResultActions actions = mvc().perform(post(URL_LOAD_GENES_HISTOGRAM).content(getObjectMapper()
.writeValueAsString(histogramQuery))
.contentType(EXPECTED_CONTENT_TYPE))
.andExpect(status().isOk())
.andExpect(content().contentType(EXPECTED_CONTENT_TYPE))
.andExpect(jsonPath(JPATH_PAYLOAD).exists())
.andExpect(jsonPath(JPATH_STATUS).value(ResultStatus.OK.name()));
actions.andDo(MockMvcResultHandlers.print());
ResponseResult<Track<Wig>> histogram = getObjectMapper()
.readValue(actions.andReturn().getResponse().getContentAsByteArray(),
getTypeFactory().constructParametrizedType(ResponseResult.class, ResponseResult.class,
getTypeFactory().constructParametrizedType(Track.class,
Track.class,
Wig.class)));
Assert.assertFalse(histogram.getPayload().getBlocks().isEmpty());
Assert.assertTrue(histogram.getPayload().getBlocks()
.stream()
.allMatch(b -> b.getStartIndex() != null && b.getEndIndex() != null && b.getValue() != null));
}
示例6: policyZoneDoesNotExistException
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
public void policyZoneDoesNotExistException() throws Exception {
// NOTE: To throw a ZoneDoesNotExistException, we must ensure that the AcsRequestContext in the
// SpringSecurityZoneResolver class returns a null ZoneEntity
MockSecurityContext.mockSecurityContext(null);
MockAcsRequestContext.mockAcsRequestContext();
String thisUri = VERSION + "/policy-set/" + this.policySet.getName();
// create policy-set in first zone
MockMvcContext putContext = this.testUtils.createWACWithCustomPUTRequestBuilder(this.wac,
this.testZone.getSubdomain(), thisUri);
ResultActions resultActions = putContext.getMockMvc().perform(
putContext.getBuilder().contentType(MediaType.APPLICATION_JSON)
.content(this.objectWriter.writeValueAsString(this.policySet)));
resultActions.andExpect(status().isUnprocessableEntity());
resultActions.andReturn().getResponse().getContentAsString().contains("zone 'null' does not exist");
MockSecurityContext.mockSecurityContext(this.testZone);
MockAcsRequestContext.mockAcsRequestContext();
}
示例7: testCreateSpeaker_conflict
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testCreateSpeaker_conflict() throws Exception {
//Given
Speaker josh = given.speaker("Josh Long").company("Pivotal").save();
String requestBody = given.asJsonString(given.speakerDto("Josh Long").company("Pivotal").build());
//When
ResultActions action = mockMvc.perform(post("/speakers").content(requestBody));
action.andDo(print());
//Then
assertEquals(1, speakerRepository.count());
action.andExpect(status().isConflict());
action.andExpect(jsonPath("content", is("Entity Speaker with name 'Josh Long' already present in DB.")));
action.andDo(document("{class-name}/{method-name}",
responseFields(
fieldWithPath("content").description("Errors that were found during validation."))));
}
示例8: loadMy
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
private List<ProjectVO> loadMy() throws Exception {
ResultActions actions = mvc()
.perform(get(URL_LOAD_MY_PROJECTS)
.contentType(EXPECTED_CONTENT_TYPE))
.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<List<ProjectVO>> myProjects = getObjectMapper()
.readValue(actions.andReturn().getResponse().getContentAsByteArray(),
getTypeFactory().constructParametrizedType(ResponseResult.class, ResponseResult.class,
getTypeFactory().constructParametrizedType(List.class,
List.class, ProjectVO.class)));
Assert.assertNotNull(myProjects.getPayload());
Assert.assertFalse(myProjects.getPayload().isEmpty());
return myProjects.getPayload();
}
示例9: testFindUsersByUsernameStartingWith
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testFindUsersByUsernameStartingWith() throws Exception {
final UserEntity user = new UserEntity()
.setId("user123")
.setEmail("[email protected]");
final Page<UserEntity> users = new PageImpl<>(Arrays.asList(user));
when(userAdminService.findUsersByUsernameStartingWith(eq("user123"), any())).thenReturn(users);
ResultActions resultActions = mockMvc.perform(get("/api/users?username=user123")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("user-read-username-startingwith"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
assertThat(response.getContentAsByteArray())
.isEqualTo(objectMapper.writeValueAsBytes(
users.map(u -> UserRestData.builder().fromUserEntity(u).build())));
verify(userAdminService).findUsersByUsernameStartingWith(eq("user123"), any());
}
示例10: testUpdateRole
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testUpdateRole() throws Exception {
RoleEntity input = new RoleEntity()
.setId("123")
.setCode("12345")
.setDescription("Description 12345");
RoleEntity dbResult = new RoleEntity()
.setId(UUID.randomUUID().toString())
.setCode("12345")
.setDescription("Description 12345");
byte[] jsonInput = objectMapper.writeValueAsBytes(RoleRestData.builder().fromRoleEntity(input).build());
when(roleService.updateRole(eq("123"), any())).thenReturn(dbResult);
ResultActions resultActions = mockMvc.perform(put("/api/roles/123")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonInput))
.andExpect(status().isOk())
.andDo(document("role-update"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
verify(roleService).updateRole(eq("123"), any());
assertThat(response.getContentAsByteArray())
.isEqualTo(objectMapper.writeValueAsBytes(RoleRestData.builder().fromRoleEntity(dbResult).build()));
}
示例11: testEnableOrDisableUser
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testEnableOrDisableUser() throws Exception {
final UserEntity user = new UserEntity()
.setEnabled(true);
when(userAdminService.enableOrDisableUser("user123", true)).thenReturn(user);
ResultActions resultActions = mockMvc.perform(put("/api/users/user123/enable")
.content("{\"enabled\": true}")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("user-enable"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
assertThat(response.getContentAsByteArray())
.isEqualTo(objectMapper.writeValueAsBytes(UserRestData.builder().fromUserEntity(user).build()));
verify(userAdminService).enableOrDisableUser("user123", true);
}
示例12: shouldPartialUpdateTodo
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void shouldPartialUpdateTodo() throws Exception {
// Given
Todo todo = new Todo(1L, "title", "desc");
given(todoService.getTodoById(1L)).willReturn(todo);
HashMap<String, Object> updates = new HashMap<>();
updates.put("title", "new title");
updates.put("description", "new description");
// When
ResultActions result = this.mvc.perform(patch("/todos/1")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(updates)));
// Then
result.andExpect(status().isNoContent())
.andExpect(status().reason("Todo partial updated!"));
}
示例13: testFindUserByEmail
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testFindUserByEmail() throws Exception {
final UserEntity user = new UserEntity()
.setId("user123")
.setEmail("[email protected]");
when(userService.findUserByEmail("[email protected]")).thenReturn(user);
ResultActions resultActions = mockMvc.perform(get("/api/users/[email protected]?email=true")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("user-read-email"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
assertThat(response.getContentAsByteArray())
.isEqualTo(objectMapper.writeValueAsBytes(UserRestData.builder().fromUserEntity(user).build()));
verify(userService).findUserByEmail("[email protected]");
}
示例14: testUpdateUserPassword
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testUpdateUserPassword() throws Exception {
final UserEntity user = new UserEntity()
.setId("user123");
when(userAdminService.updateUserPassword(eq("user123"), any())).thenReturn(user);
ResultActions resultActions = mockMvc.perform(put("/api/users/user123/password")
.content("{\"username\": \"user123\"}")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("user-update-password"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
assertThat(response.getContentAsByteArray())
.isEqualTo(objectMapper.writeValueAsBytes(UserRestData.builder().fromUserEntity(user).build()));
verify(userAdminService).updateUserPassword(eq("user123"), any());
}
示例15: testRemovePrivilegeFromRole
import org.springframework.test.web.servlet.ResultActions; //导入依赖的package包/类
@Test
public void testRemovePrivilegeFromRole() throws Exception {
final RoleEntity role = new RoleEntity()
.setId("role123")
.setCode("role123");
when(roleService.removePrivilegeFromRole("role123", "privilege123")).thenReturn(role);
ResultActions resultActions = mockMvc.perform(delete("/api/roles/role123/privileges")
.content("{\"privilege\": \"privilege123\"}")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("role-privilege-delete"));
MockHttpServletResponse response = resultActions
.andReturn()
.getResponse();
verify(roleService).removePrivilegeFromRole("role123", "privilege123");
assertThat(response.getContentAsByteArray())
.isEqualTo(objectMapper.writeValueAsBytes(RoleRestData.builder().fromRoleEntity(role).build()));
}