### Question:
SecuredController extends UIController { @Override protected final ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { ApplicationState state = getApplicationState(request); User currentUser = state.getCurrentUser(); if (currentUser != null) { return super.processFormSubmission(request, response, command, errors); } state.setPreviousAction(request.getRequestURL().toString()); return Constants.REDIRECTION_TO_LOGIN; }  static String getCurrentUserName(HttpServletRequest request);  }### Answer:
@Test public void testProcessFormSubmissionWithoutUser() throws Exception { securedController = (SecuredController) context.getBean("tasksController"); ModelAndView mav = securedController.processFormSubmission(request, response, null, null); Assert.assertTrue(mav.getView().toString().contains(Constants.LOGIN_URL)); }
@Test public void testProcessFormSubmissionWithUser() throws Exception { FakeUIFWApplicationState state = new FakeUIFWApplicationState(); User currentUser = new User("test1", new String[] { "test/test1" }, "token1"); state.setCurrentUser(currentUser); request.getSession().setAttribute("APPLICATION_STATE", state); securedController = (SecuredController) context.getBean("tasksController"); ModelAndView mav = securedController.processFormSubmission(request, response, null, new BindException(currentUser, "test")); Assert.assertEquals(mav.getViewName(), "tasks"); }
@Test public void testProcessFormSubmissionInvalidActionWithUser() throws Exception { MockHttpServletRequest mockRequest = new MockHttpServletRequest(); mockRequest.addParameter("actionName", "getTaskList"); request = mockRequest; FakeUIFWApplicationState state = new FakeUIFWApplicationState(); User currentUser = new User("test1", new String[] { "test/test1" }, "token1"); state.setCurrentUser(currentUser); request.getSession().setAttribute("APPLICATION_STATE", state); securedController = (SecuredController) context.getBean("tasksController"); try { ModelAndView mav = securedController.processFormSubmission(request, response, null, new BindException(currentUser, "test")); Assert.fail("Invalid Action exception expected"); } catch (Exception e) { } }
@Test public void testProcessFormSubmissionValidActionWithUser() throws Exception { MockHttpServletRequest mockRequest = new MockHttpServletRequest(); mockRequest.addParameter("actionName", "default"); request = mockRequest; FakeUIFWApplicationState state = new FakeUIFWApplicationState(); User currentUser = new User("test1", new String[] { "test/test1" }, "token1"); state.setCurrentUser(currentUser); request.getSession().setAttribute("APPLICATION_STATE", state); securedController = (SecuredController) context.getBean("tasksController"); ModelAndView mav = securedController.processFormSubmission(request, response, null, new BindException(currentUser, "test")); Assert.assertEquals(mav.getViewName(), "tasks"); }