Blame view

src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java 1.76 KB
aa193b2b   Julien Iguchi-Cartigny   First version
1
2
  package fr.plil.sio.web.mvc;
  
aa193b2b   Julien Iguchi-Cartigny   First version
3
4
5
6
7
8
  import org.junit.Before;
  import org.junit.Test;
  import org.springframework.validation.BeanPropertyBindingResult;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.servlet.ModelAndView;
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
9
  import static org.junit.Assert.*;
2afad654   Julien Iguchi-Cartigny   deal with userForm
10
  import static org.mockito.Mockito.*;
2070653e   Julien Iguchi-Cartigny   Switching to spri...
11
  
aa193b2b   Julien Iguchi-Cartigny   First version
12
13
14
15
  public class NewUserControllerTest {
  
      private NewUserController newUserController;
      private BindingResult results;
322c9502   Julien Cartigny   Ad Spring securit...
16
      private UserForm userForm;
2afad654   Julien Iguchi-Cartigny   deal with userForm
17
      private UserService userService;
aa193b2b   Julien Iguchi-Cartigny   First version
18
19
20
21
  
      @Before
      public void createInstances() {
          newUserController = new NewUserController();
322c9502   Julien Cartigny   Ad Spring securit...
22
23
24
25
          userForm = new UserForm();
          results = new BeanPropertyBindingResult(userForm, "user");
          userService = mock(UserService.class);
          User user = new User();
ed4910db   Julien Cartigny   Fixing tests
26
27
          user.setUsername("admin");
          user.setUsername("blabla");
322c9502   Julien Cartigny   Ad Spring securit...
28
29
          when(userService.findByUsername("admin")).thenReturn(user);
          newUserController.setUserService(userService);
2afad654   Julien Iguchi-Cartigny   deal with userForm
30
31
          userService = mock(UserService.class);
          newUserController.setUserService(userService);
aa193b2b   Julien Iguchi-Cartigny   First version
32
33
34
35
36
37
38
      }
  
      @Test
      public void testGetNewUserForm() {
          ModelAndView mav = newUserController.getNewUserForm();
          assertEquals("newUser", mav.getViewName());
          assertEquals(1, mav.getModelMap().size());
ed4910db   Julien Cartigny   Fixing tests
39
40
          assertTrue(mav.getModel().containsKey("userForm"));
          assertTrue(mav.getModel().get("userForm") instanceof UserForm);
aa193b2b   Julien Iguchi-Cartigny   First version
41
42
43
44
      }
  
      @Test
      public void testPostNewUserSucceed() {
322c9502   Julien Cartigny   Ad Spring securit...
45
46
47
          userForm.setUsername("abc");
          userForm.setPassword("abcD#");
          String view = newUserController.postNewUser(userForm, results);
aa193b2b   Julien Iguchi-Cartigny   First version
48
          assertFalse(results.hasErrors());
2afad654   Julien Iguchi-Cartigny   deal with userForm
49
50
          assertEquals("redirect:/", view);
          verify(userService).createUser("abc", "abcD#");
aa193b2b   Julien Iguchi-Cartigny   First version
51
      }
aa193b2b   Julien Iguchi-Cartigny   First version
52
  }