Commit 3c085ac4872d238673b1802b7bb301d102a4b971

Authored by Julien Iguchi-Cartigny
1 parent 2afad654

Support for mockMVC + UserService integration test

@@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
14 <groupId>org.springframework.boot</groupId> 14 <groupId>org.springframework.boot</groupId>
15 <artifactId>spring-boot-starter-parent</artifactId> 15 <artifactId>spring-boot-starter-parent</artifactId>
16 <version>1.2.4.RELEASE</version> 16 <version>1.2.4.RELEASE</version>
17 - <relativePath/> <!-- lookup parent from repository --> 17 + <relativePath/>
18 </parent> 18 </parent>
19 19
20 <properties> 20 <properties>
src/test/java/fr/plil/sio/web/mvc/UserServiceTest.java 0 → 100644
@@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
  1 +package fr.plil.sio.web.mvc;
  2 +
  3 +import org.junit.Test;
  4 +import org.junit.runner.RunWith;
  5 +import org.springframework.boot.test.SpringApplicationConfiguration;
  6 +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7 +import org.springframework.test.context.transaction.TransactionConfiguration;
  8 +import org.springframework.transaction.annotation.Transactional;
  9 +
  10 +import javax.annotation.Resource;
  11 +
  12 +import static org.junit.Assert.assertEquals;
  13 +
  14 +@RunWith(SpringJUnit4ClassRunner.class)
  15 +@SpringApplicationConfiguration(classes = Application.class)
  16 +@Transactional
  17 +@TransactionConfiguration
  18 +public class UserServiceTest {
  19 +
  20 + @Resource
  21 + private UserService userService;
  22 +
  23 + @Resource
  24 + private UserRepository userRepository;
  25 +
  26 + @Test
  27 + public void testCreateUser() {
  28 + userService.createUser("blabla", "polo");
  29 + assertEquals("polo", userRepository.findByUsername("blabla").getPassword());
  30 + }
  31 +}
src/test/java/fr/plil/sio/web/mvc/ViewUsersControllerTest.java
@@ -2,23 +2,36 @@ package fr.plil.sio.web.mvc; @@ -2,23 +2,36 @@ package fr.plil.sio.web.mvc;
2 2
3 import org.junit.Before; 3 import org.junit.Before;
4 import org.junit.Test; 4 import org.junit.Test;
  5 +import org.junit.runner.RunWith;
  6 +import org.springframework.boot.test.SpringApplicationConfiguration;
  7 +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8 +import org.springframework.test.context.web.WebAppConfiguration;
  9 +import org.springframework.test.web.servlet.MockMvc;
  10 +import org.springframework.test.web.servlet.setup.MockMvcBuilders;
5 11
6 import java.util.LinkedList; 12 import java.util.LinkedList;
7 import java.util.List; 13 import java.util.List;
8 14
9 -import static org.junit.Assert.assertEquals;  
10 import static org.mockito.Mockito.mock; 15 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when; 16 import static org.mockito.Mockito.when;
  17 +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  18 +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
12 19
  20 +@RunWith(SpringJUnit4ClassRunner.class)
  21 +@SpringApplicationConfiguration(classes = Application.class)
  22 +@WebAppConfiguration
13 public class ViewUsersControllerTest { 23 public class ViewUsersControllerTest {
14 24
15 private ViewUsersController viewUsersController; 25 private ViewUsersController viewUsersController;
16 private UserRepository userRepository; 26 private UserRepository userRepository;
17 private UserSession userSession; 27 private UserSession userSession;
18 28
  29 + private MockMvc mockMvc;
  30 +
19 @Before 31 @Before
20 public void createInstances() { 32 public void createInstances() {
21 viewUsersController = new ViewUsersController(); 33 viewUsersController = new ViewUsersController();
  34 + mockMvc = MockMvcBuilders.standaloneSetup(viewUsersController).build();
22 userRepository = mock(UserRepository.class); 35 userRepository = mock(UserRepository.class);
23 List<User> users = new LinkedList<>(); 36 List<User> users = new LinkedList<>();
24 users.add(new User("admin", "password")); 37 users.add(new User("admin", "password"));
@@ -30,17 +43,11 @@ public class ViewUsersControllerTest { @@ -30,17 +43,11 @@ public class ViewUsersControllerTest {
30 } 43 }
31 44
32 @Test 45 @Test
33 - public void testPopulateUsers() {  
34 - assertEquals(1, viewUsersController.populateUsers().size());  
35 - }  
36 -  
37 - @Test  
38 - public void testPopulateUser() {  
39 - assertEquals("admin", viewUsersController.populateUser().getUsername());  
40 - }  
41 -  
42 - @Test  
43 - public void testGetViewUsers() {  
44 - assertEquals("viewUsers",viewUsersController.getViewUsers()); 46 + public void testPopulateUsers() throws Exception {
  47 + mockMvc.perform(get("/"))
  48 + .andExpect(status().isOk())
  49 + .andExpect(view().name("viewUsers"))
  50 + .andExpect(model().attributeExists("users"))
  51 + .andExpect(model().attributeExists("userSession"));
45 } 52 }
46 } 53 }