Blame view

src/test/java/fr/plil/sio/web/mvc/ViewUsersControllerTest.java 1.79 KB
aa193b2b   Julien Iguchi-Cartigny   First version
1
2
  package fr.plil.sio.web.mvc;
  
aa193b2b   Julien Iguchi-Cartigny   First version
3
4
  import org.junit.Before;
  import org.junit.Test;
3c085ac4   Julien Iguchi-Cartigny   Support for mockM...
5
  import org.junit.runner.RunWith;
88ff471f   Julien Cartigny   Switching to spri...
6
  import org.springframework.boot.test.context.SpringBootTest;
3c085ac4   Julien Iguchi-Cartigny   Support for mockM...
7
8
9
10
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.web.WebAppConfiguration;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.setup.MockMvcBuilders;
aa193b2b   Julien Iguchi-Cartigny   First version
11
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
12
13
14
  import java.util.LinkedList;
  import java.util.List;
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
15
16
  import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.when;
3c085ac4   Julien Iguchi-Cartigny   Support for mockM...
17
18
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2070653e   Julien Iguchi-Cartigny   Switching to spri...
19
  
3c085ac4   Julien Iguchi-Cartigny   Support for mockM...
20
  @RunWith(SpringJUnit4ClassRunner.class)
88ff471f   Julien Cartigny   Switching to spri...
21
  @SpringBootTest(classes = Application.class)
3c085ac4   Julien Iguchi-Cartigny   Support for mockM...
22
  @WebAppConfiguration
aa193b2b   Julien Iguchi-Cartigny   First version
23
24
25
  public class ViewUsersControllerTest {
  
      private ViewUsersController viewUsersController;
8efec875   Julien Iguchi-Cartigny   switch to spring ...
26
      private UserRepository userRepository;
aa193b2b   Julien Iguchi-Cartigny   First version
27
  
3c085ac4   Julien Iguchi-Cartigny   Support for mockM...
28
29
      private MockMvc mockMvc;
  
aa193b2b   Julien Iguchi-Cartigny   First version
30
31
32
      @Before
      public void createInstances() {
          viewUsersController = new ViewUsersController();
3c085ac4   Julien Iguchi-Cartigny   Support for mockM...
33
          mockMvc = MockMvcBuilders.standaloneSetup(viewUsersController).build();
2070653e   Julien Iguchi-Cartigny   Switching to spri...
34
35
          userRepository = mock(UserRepository.class);
          List<User> users = new LinkedList<>();
322c9502   Julien Cartigny   Ad Spring securit...
36
37
38
39
          User testUser = new User();
          testUser.setPassword("admin");
          testUser.setUsername("password");
          users.add(testUser);
2070653e   Julien Iguchi-Cartigny   Switching to spri...
40
          when(userRepository.findAll()).thenReturn(users);
8efec875   Julien Iguchi-Cartigny   switch to spring ...
41
          viewUsersController.setUserRepository(userRepository);
aa193b2b   Julien Iguchi-Cartigny   First version
42
43
44
      }
  
      @Test
3c085ac4   Julien Iguchi-Cartigny   Support for mockM...
45
46
47
48
      public void testPopulateUsers() throws Exception {
          mockMvc.perform(get("/"))
                  .andExpect(status().isOk())
                  .andExpect(view().name("viewUsers"))
322c9502   Julien Cartigny   Ad Spring securit...
49
                  .andExpect(model().attributeExists("users"));
aa193b2b   Julien Iguchi-Cartigny   First version
50
51
      }
  }