|
@@ -1,16 +1,76 @@
|
1
|
1
|
package io.zipcoder.crudapp;
|
2
|
2
|
|
|
3
|
+import org.junit.Before;
|
3
|
4
|
import org.junit.Test;
|
4
|
5
|
import org.junit.runner.RunWith;
|
|
6
|
+import org.mockito.InjectMocks;
|
|
7
|
+import org.mockito.Mock;
|
|
8
|
+import org.mockito.MockitoAnnotations;
|
5
|
9
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
10
|
+import org.springframework.http.MediaType;
|
6
|
11
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
12
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
13
|
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
|
14
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
15
|
+
|
|
16
|
+import java.util.Arrays;
|
|
17
|
+import java.util.List;
|
|
18
|
+
|
|
19
|
+import static org.hamcrest.Matchers.*;
|
|
20
|
+import static org.mockito.Mockito.*;
|
|
21
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
22
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
|
23
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
24
|
+
|
7
|
25
|
|
8
|
26
|
@RunWith(SpringRunner.class)
|
9
|
27
|
@SpringBootTest
|
10
|
28
|
public class CRUDApplicationTests {
|
11
|
29
|
|
|
30
|
+ private MockMvc mockMvc;
|
|
31
|
+
|
|
32
|
+ @Mock
|
|
33
|
+ private PersonService personService;
|
|
34
|
+
|
|
35
|
+ @InjectMocks
|
|
36
|
+ private PersonController personController;
|
|
37
|
+
|
|
38
|
+ @Before
|
|
39
|
+ public void init(){
|
|
40
|
+ MockitoAnnotations.initMocks(this);
|
|
41
|
+ mockMvc = MockMvcBuilders
|
|
42
|
+ .standaloneSetup(personController)
|
|
43
|
+ .build();
|
|
44
|
+ }
|
|
45
|
+
|
12
|
46
|
@Test
|
13
|
47
|
public void contextLoads() {
|
14
|
48
|
}
|
15
|
49
|
|
|
50
|
+ @Test
|
|
51
|
+ public void test_get_all_success() throws Exception {
|
|
52
|
+ List<Person> personList = Arrays.asList(
|
|
53
|
+ new Person("Daenerys", "Targaryen"),
|
|
54
|
+ new Person("John", "Snow"));
|
|
55
|
+
|
|
56
|
+ when(personService.getAll()).thenReturn(personList);
|
|
57
|
+
|
|
58
|
+ mockMvc.perform(get("/person"))
|
|
59
|
+ .andExpect(status().isOk())
|
|
60
|
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
|
|
61
|
+ .andExpect(MockMvcResultMatchers.jsonPath("$", hasSize(2)))
|
|
62
|
+
|
|
63
|
+ .andExpect(MockMvcResultMatchers.jsonPath("$[0].firstName", is("Daenerys")))
|
|
64
|
+ .andExpect(MockMvcResultMatchers.jsonPath("$[0].firstName", is("Targaryen")))
|
|
65
|
+ .andExpect(MockMvcResultMatchers.jsonPath("$[0].id", is(1)))
|
|
66
|
+
|
|
67
|
+ .andExpect(MockMvcResultMatchers.jsonPath("$[1].firstName", is("John")))
|
|
68
|
+ .andExpect(MockMvcResultMatchers.jsonPath("$[0].firstName", is("Snow")))
|
|
69
|
+ .andExpect(MockMvcResultMatchers.jsonPath("$[1].id", is(2)))
|
|
70
|
+ ;
|
|
71
|
+
|
|
72
|
+ verify(personService, times(1)).getAll();
|
|
73
|
+ verifyNoMoreInteractions(personService);
|
|
74
|
+ }
|
|
75
|
+
|
16
|
76
|
}
|