UserController { @RequestMapping(value = "/login", method = RequestMethod.POST) public User login(@Validated(Authenticate.class) @RequestBody User user, HttpServletResponse response) { user = userService.loginUser(user); if (user == null) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); return null; } else { user.setSessionId(""); return user; } } @Autowired  UserController(UserService userService);  UserController(); @RequestMapping(value = "/registration", method = RequestMethod.POST) User register(@Validated(Create.class) @RequestBody User user); @RequestMapping(value = "/login", method = RequestMethod.POST) User login(@Validated(Authenticate.class) @RequestBody User user, HttpServletResponse response); void setService(UserService service);  }
@Test public void testValidLogin(){ when(userService.loginUser(user)).thenReturn(user); assertEquals(user, userController.login(user, response)); }
@Test public void testInvalidLogin(){ assertEquals(null, userController.login(user2, response)); }