Ejemplo n.º 1
1
 @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();
 }
Ejemplo n.º 2
0
 @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 getHTML() throws Exception {
    /*
       This following code will do 'GET' to the web apps
       and check that the return view is "helloworld"
       and also that it has a attribute "user" to "JohnathanMarkSmith"

    */
    this.mockMvc
        .perform(get("/ask/JohnathanMarkSmith").accept(MediaType.TEXT_HTML))
        .andExpect(status().isOk())
        .andExpect(view().name("helloworld"))
        .andExpect(MockMvcResultMatchers.model().attribute("user", "JohnathanMarkSmith"));
  }
  @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());
  }