Source:
public void testNoSuchUserException() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); final HttpServletResponse response = mock(HttpServletResponse.class); final HttpSession session = mock(HttpSession.class); final LoginService loginService = mock(LoginService.class); final UserLocalManager userLocalManager = mock(UserLocalManager.class); final ResponseUtil authenticationHelper = ResponseUtil.INSTANCE; final String userId = "admin@dotcms.com"; final String pass = "pass"; final ServletContext context = mock(ServletContext.class); final JsonWebTokenUtils jsonWebTokenUtils = mock(JsonWebTokenUtils.class); final SecurityLoggerServiceAPI securityLoggerServiceAPI = mock(SecurityLoggerServiceAPI.class); Config.CONTEXT = context; when(context.getInitParameter("company_id")).thenReturn(User.DEFAULT); when(request.getSession()).thenReturn(session); when(loginService.doActionLogin( userId, pass, false, request, response)) .thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { throw new NoSuchUserException(); } }); final CreateJsonWebTokenResource createJsonWebTokenResource = new CreateJsonWebTokenResource(loginService, userLocalManager, authenticationHelper, jsonWebTokenUtils, securityLoggerServiceAPI); final CreateTokenForm createTokenForm = new CreateTokenForm.Builder().user(userId).password(pass).build(); final Response response1 = createJsonWebTokenResource.getApiToken(request, response, createTokenForm); assertNotNull(response1); assertEquals(response1.getStatus(), 401); assertNotNull(response1.getEntity()); assertTrue(response1.getEntity() instanceof ResponseEntityView); assertNotNull(ResponseEntityView.class.cast(response1.getEntity()).getErrors()); assertTrue(ResponseEntityView.class.cast(response1.getEntity()).getErrors().size() > 0); assertNotNull(ResponseEntityView.class.cast(response1.getEntity()).getErrors().get(0)); assertTrue(ResponseEntityView.class.cast(response1.getEntity()).getErrors().get(0).getErrorCode().equals("authentication-failed")); }

Target:
public void testNoSuchUserException() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); final HttpServletResponse response = mock(HttpServletResponse.class); final HttpSession session = mock(HttpSession.class); final LoginService loginService = mock(LoginService.class); final UserLocalManager userLocalManager = mock(UserLocalManager.class); final ResponseUtil responseUtil = ResponseUtil.INSTANCE; final String userId = "admin@dotcms.com"; final String pass = "pass"; final ServletContext context = mock(ServletContext.class); final JsonWebTokenUtils jsonWebTokenUtils = mock(JsonWebTokenUtils.class); final SecurityLoggerServiceAPI securityLoggerServiceAPI = mock(SecurityLoggerServiceAPI.class); Config.CONTEXT = context; when(context.getInitParameter("company_id")).thenReturn(RestUtilTest.DEFAULT_COMPANY); when(request.getSession()).thenReturn(session); when(loginService.doActionLogin( userId, pass, false, request, response)) .thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { throw new NoSuchUserException(); } }); final CreateJsonWebTokenResource createJsonWebTokenResource = new CreateJsonWebTokenResource(loginService, userLocalManager, responseUtil, jsonWebTokenUtils, securityLoggerServiceAPI); final CreateTokenForm createTokenForm = new CreateTokenForm.Builder().user(userId).password(pass).build(); final Response response1 = createJsonWebTokenResource.getApiToken(request, response, createTokenForm); assertNotNull(response1); assertEquals(response1.getStatus(), 401); assertNotNull(response1.getEntity()); assertTrue(response1.getEntity() instanceof ResponseEntityView); assertNotNull(ResponseEntityView.class.cast(response1.getEntity()).getErrors()); assertTrue(ResponseEntityView.class.cast(response1.getEntity()).getErrors().size() > 0); assertNotNull(ResponseEntityView.class.cast(response1.getEntity()).getErrors().get(0)); assertTrue(ResponseEntityView.class.cast(response1.getEntity()).getErrors().get(0).getErrorCode().equals("authentication-failed")); }