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