main method:
@RequestMapping(value="/getAllUsers",method=RequestMethod.GET)
public List<UserRoleAssoc> getAllUsers() {
List<Users> userLst = (List<Users>) userRepository.findAll();
final List<UserRoleAssoc> userRoleAssociationList = new ArrayList<UserRoleAssoc>();
for(Users users : userLst) {
List<UserRoleAssoc> userRoleAssoList = userRoleAssociationRepository.getRoleByuserId(users.getUserId());
if (userRoleAssoList != null)
{
for (UserRoleAssoc userRoleList : userRoleAssoList) {
userRoleAssociationList.add(userRoleList);
}
}
}
return userRoleAssociationList;
}
test case:
/* start getAllUsers*/
@Test
public void getAllUserstest() throws Exception {
MockHttpServletResponse response = mvc.perform(get(restEndPoints.GET_ALL_USERS.uri() + 987)
.contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertNotNull(response.getContentAsString());}
我实际上无法编写这些测试用例,使用findall的GET和POst方法很难覆盖