/** Test the home page controller. */ @Test public void testHome() { GenericServiceMockup<Person> personService = new GenericServiceMockup<Person>(); homeController.setPrivilegeEvaluator(mockup); homeController.setPersonBean(personService); ModelAndView result = homeController.home(); assertEquals( "Wrong view name for exception page", HomeController.HOME_VIEW, result.getViewName()); }
@Test public void testController() { HomeController controller = new HomeController(); Model model = new ExtendedModelMap(); Assert.assertEquals("home", controller.home(model)); Object message = model.asMap().get("controllerMessage"); Assert.assertEquals("This is the message from the controller!", message); }
/** * Test method for {@link org.sagar.samples.controller.spring.HomeController#home()}. * * @throws Exception */ @Test public void testHome() throws Exception { System.out.println("excecuting test method 1"); HomeController homeController = new HomeController(); ModelAndView mnv = homeController.home(); assertEquals(mnv.getViewName(), "home"); assertEquals((String) mnv.getModel().get("name"), "Sagar Prasad"); // ------------MVC TESTING------------------// mockMvc .perform(MockMvcRequestBuilders.get("/home").accept(MediaType.APPLICATION_XML)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(print()); ; }
@Test(expected = Exception.class) public void loginReturnsAlertIfWrongEmailOrPassword() { doThrow(new Exception()) .when(userServiceMock) .validateCredentials(USER_EMAIL, USER_WRONG_PASSWORD); homeController.login(loginFormViewModelMock, modelMapMock, sessionMock, servletRequestMock); verify(modelMapMock, times(1)).addAttribute(ALERT_ATTRIBUTE, ALERT_MESSAGE); }
@Test public void loginReturnsCorrectModelForm() { when(loginFormViewModelMock.getEmail()).thenReturn(USER_EMAIL); when(loginFormViewModelMock.getPassword()).thenReturn(USER_PASSWORD); ModelAndView returnedModel = homeController.login(loginFormViewModelMock, modelMapMock, sessionMock, servletRequestMock); assertEquals("home", returnedModel.getViewName()); }
@Test public void loginSetsSessionIdleTreshold() { when(loginFormViewModelMock.getEmail()).thenReturn(USER_EMAIL); when(loginFormViewModelMock.getPassword()).thenReturn(USER_PASSWORD); when(userServiceMock.retrieveUserRole(USER_EMAIL)).thenReturn(USER_ROLE); when(userServiceMock.validateCredentials(USER_EMAIL, USER_PASSWORD)).thenReturn(true); when(servletRequestMock.getSession()).thenReturn(sessionMock); homeController.login(loginFormViewModelMock, modelMapMock, sessionMock, servletRequestMock); verify(sessionMock, times(1)).setMaxInactiveInterval(SESSION_IDLE_TRESHOLD_IN_SECONDS); }
@Test public void logoutRedirectsWhenSuccessful() { String returnedForm = homeController.logout(sessionStatusMock, modelMapMock); assertEquals(SIMPLE_REDIRECT, returnedForm); }
@Test public void showLoginFormReturnsHomeViewForm() { String returnedForm = homeController.showLoginForm(); assertEquals(HOME_VIEW, returnedForm); }
@Test public void defaultUserReturnsNewLoginFormViewModel() { LoginFormViewModel returnedModel = homeController.defaultUser(); assertNotNull(returnedModel); }
@Test @WithUserDetails("ewalsh") public void testFour() { homeController.showHomepage(new ModelMap()); }
@Test(expected = AccessDeniedException.class) @WithMockUser(username = "******") public void testThree() { homeController.showHomepage(new ModelMap()); }
@Test(expected = AccessDeniedException.class) @WithMockUser(roles = {"ADMIN"}) public void testTwo() { homeController.showHomepage(new ModelMap()); }
@Test(expected = AuthenticationCredentialsNotFoundException.class) public void testOne() { homeController.showHomepage(new ModelMap()); }