Blame view

src/test/java/fr/plil/sio/web/mvc/NewUserControllerTest.java 2.54 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
26
27
          userForm = new UserForm();
          results = new BeanPropertyBindingResult(userForm, "user");
          userService = mock(UserService.class);
          User user = new User();
          when(userService.findByUsername("admin")).thenReturn(user);
          newUserController.setUserService(userService);
2afad654   Julien Iguchi-Cartigny   deal with userForm
28
29
          userService = mock(UserService.class);
          newUserController.setUserService(userService);
aa193b2b   Julien Iguchi-Cartigny   First version
30
31
32
33
34
35
36
37
38
39
40
41
42
      }
  
      @Test
      public void testGetNewUserForm() {
          ModelAndView mav = newUserController.getNewUserForm();
          assertEquals("newUser", mav.getViewName());
          assertEquals(1, mav.getModelMap().size());
          assertTrue(mav.getModel().containsKey("user"));
          assertTrue(mav.getModel().get("user") instanceof User);
      }
  
      @Test
      public void testPostNewUserSucceed() {
322c9502   Julien Cartigny   Ad Spring securit...
43
44
45
          userForm.setUsername("abc");
          userForm.setPassword("abcD#");
          String view = newUserController.postNewUser(userForm, results);
aa193b2b   Julien Iguchi-Cartigny   First version
46
          assertFalse(results.hasErrors());
2afad654   Julien Iguchi-Cartigny   deal with userForm
47
48
          assertEquals("redirect:/", view);
          verify(userService).createUser("abc", "abcD#");
aa193b2b   Julien Iguchi-Cartigny   First version
49
50
51
52
      }
  
      @Test
      public void testPostNewUserFailedNotAdmin() {
322c9502   Julien Cartigny   Ad Spring securit...
53
54
55
          userForm.setUsername("abc");
          userForm.setPassword("abcD#");
          String view = newUserController.postNewUser(userForm, results);
aa193b2b   Julien Iguchi-Cartigny   First version
56
57
58
59
60
61
          assertTrue(results.hasErrors());
          assertEquals("newUser",view);
      }
  
      @Test
      public void testPostNewUserFailedValidate() {
322c9502   Julien Cartigny   Ad Spring securit...
62
63
64
          userForm.setUsername("a");
          userForm.setPassword("abc");
          String view = newUserController.postNewUser(userForm, results);
aa193b2b   Julien Iguchi-Cartigny   First version
65
66
67
68
69
70
          assertTrue(results.hasErrors());
          assertEquals("newUser",view);
      }
  
      @Test
      public void testPostNewUserFailedAlreadyPresent() {
322c9502   Julien Cartigny   Ad Spring securit...
71
72
73
          userForm.setUsername("admin");
          userForm.setPassword("blabla");
          String view = newUserController.postNewUser(userForm, results);
aa193b2b   Julien Iguchi-Cartigny   First version
74
75
76
77
          assertTrue(results.hasErrors());
          assertEquals("newUser",view);
      }
  }