UserController implements Serializable { @RequestMapping(method=RequestMethod.GET) public ResponseEntity<List<User>> findAll(){ final List<User> users = userService.getAllUsers(); if(users == null){ return new ResponseEntity<List<User>>(HttpStatus.NOT_FOUND); } return new ResponseEntity<List<User>>(users, HttpStatus.OK); } @Autowired  UserController(UserService userService); @RequestMapping(method=RequestMethod.GET) ResponseEntity<List<User>> findAll(); @RequestMapping(method = RequestMethod.POST) ResponseEntity<User> createUser(@RequestBody User user); @RequestMapping(value="/{id}",method = RequestMethod.GET) ResponseEntity<User> getUser(@PathVariable String id); @RequestMapping(value="/{id}",method = RequestMethod.PUT) ResponseEntity<User> updateUser(@PathVariable String id, @RequestBody User user); @RequestMapping(value="/{id}",method = RequestMethod.DELETE) ResponseEntity<User> deleteUser(@PathVariable String id, @RequestBody User user);  }
@Test public void testFindAll() throws Exception{ final User user1 = new User(); user1.setId("1"); user1.setEmail("bhdrkb@gmail.com"); user1.setUsername("bhdrkn"); user1.setPassword("1q2w3e"); final User user2 = new User(); user2.setId("2"); user2.setEmail("bhdrkb2@gmail.com"); user2.setUsername("bhdrkn2"); user2.setPassword("1q2w3e"); final List<User> users = new ArrayList<User>(); users.add(user1); users.add(user2); Mockito.when(userService.getAllUsers()).thenReturn(users); mockMvc.perform(MockMvcRequestBuilders.get("/rest/users")) .andExpect(MockMvcResultMatchers.jsonPath("$.[*].username", Matchers.hasItems( Matchers.endsWith("bhdrkn"), Matchers.endsWith("bhdrkn2")))) .andExpect(MockMvcResultMatchers.status().isOk()); }