@Test public void whenHasValidationFailsShouldReturnToRegisterForm() throws Exception { SpitterRepository spittlerRepository = Mockito.mock(SpitterRepository.class); SpitterController spittleController = new SpitterController(spittlerRepository); MockMvc mockSpittleController = MockMvcBuilders.standaloneSetup(spittleController).build(); mockSpittleController .perform( MockMvcRequestBuilders.post("/spitter/registerNoPic") .param("userName", "gdiaz") .param("password", "gd123")) .andExpect(MockMvcResultMatchers.view().name("registerForm")); }
@Test public void postAccountTest() throws Exception { mockMvc .perform( MockMvcRequestBuilders.post("/account") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .param("name", accountForm.getName()) .param("surname", accountForm.getSurname()) .param("address", accountForm.getAddress()) .param("phone", accountForm.getPhone().toString()) .param("iban", accountForm.getIban().toString()) .param("balance", accountForm.getBalance().toString()) .param("currency", accountForm.getCurrency())) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.view().name("account")) .andExpect(MockMvcResultMatchers.model().attributeExists("currencies")) .andReturn(); }
@Test public void getAccountTest() throws Exception { mockMvc .perform(MockMvcRequestBuilders.get("/account")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.view().name("account")) .andExpect(MockMvcResultMatchers.model().attributeExists("currencies")) .andReturn(); }
@Test public void testView() throws Exception { MvcResult result = mockMvc .perform(MockMvcRequestBuilders.get("/user/1")) .andExpect(MockMvcResultMatchers.view().name("user/view")) .andExpect(MockMvcResultMatchers.model().attributeExists("user")) .andDo(MockMvcResultHandlers.print()) .andReturn(); Assert.assertNotNull(result.getModelAndView().getModel().get("user")); }
@Test public void testShowRegisterForm() throws Exception { SpitterRepository spittleRepository = Mockito.mock(SpitterRepository.class); SpitterController spittlerController = new SpitterController(spittleRepository); MockMvc mockSpittleController = MockMvcBuilders.standaloneSetup(spittlerController).build(); mockSpittleController .perform(MockMvcRequestBuilders.get("/spitter/register")) .andExpect(MockMvcResultMatchers.model().attributeExists("spitter")) .andExpect(MockMvcResultMatchers.view().name("registerForm")); }
@Test public void whenFlashAttributeIsPassedRepositoryIsNotCalled() throws Exception { SpitterRepository fakeSpitterRepository = Mockito.mock(SpitterRepository.class); Mockito.when(fakeSpitterRepository.findByUsername(Mockito.anyString())) .thenReturn(new Spitter()); SpitterController spitterController = new SpitterController(fakeSpitterRepository); MockMvc mockSpitterController = MockMvcBuilders.standaloneSetup(spitterController).build(); mockSpitterController .perform(MockMvcRequestBuilders.get("/spitter/gdiaz").flashAttr("spitter", new Spitter())) .andExpect(MockMvcResultMatchers.model().attributeExists("spitter")) .andExpect(MockMvcResultMatchers.view().name("profile")); Mockito.verify(fakeSpitterRepository, Mockito.never()).findByUsername(Mockito.anyString()); }
@Test public void testShowProfilePage() throws Exception { Spitter spitterUser = new Spitter(1L, "name", "secondname", "gdiaz", ""); SpitterRepository spitterRepository = Mockito.mock(SpitterRepository.class); Mockito.when(spitterRepository.findByUsername(spitterUser.getUserName())) .thenReturn(spitterUser); SpitterController spitterController = new SpitterController(spitterRepository); MockMvc mockSpitterController = MockMvcBuilders.standaloneSetup(spitterController).build(); mockSpitterController .perform(MockMvcRequestBuilders.get("/spitter/" + spitterUser.getUserName())) .andExpect(MockMvcResultMatchers.model().attributeExists("spitter")) .andExpect(MockMvcResultMatchers.view().name("profile")); Mockito.verify(spitterRepository, Mockito.atLeastOnce()) .findByUsername(spitterUser.getUserName()); }