|
@@ -3,17 +3,25 @@ package io.zipcoder.crudapp;
|
3
|
3
|
import org.junit.Assert;
|
4
|
4
|
import org.junit.Before;
|
5
|
5
|
import org.junit.Test;
|
|
6
|
+import org.mockito.InjectMocks;
|
6
|
7
|
import org.mockito.Mock;
|
7
|
8
|
import org.mockito.MockitoAnnotations;
|
8
|
9
|
import org.springframework.http.HttpStatus;
|
9
|
10
|
import org.springframework.http.ResponseEntity;
|
|
11
|
+
|
|
12
|
+import java.util.ArrayList;
|
|
13
|
+
|
10
|
14
|
import static org.mockito.Matchers.any;
|
11
|
15
|
import static org.mockito.Mockito.when;
|
12
|
16
|
|
13
|
17
|
public class PersonControllerTest {
|
14
|
18
|
|
15
|
19
|
@Mock
|
16
|
|
- private PersonController controller;
|
|
20
|
+ private PersonRepository repository;
|
|
21
|
+
|
|
22
|
+ @InjectMocks
|
|
23
|
+ private PersonController controller = new PersonController();
|
|
24
|
+
|
17
|
25
|
|
18
|
26
|
@Before
|
19
|
27
|
public void setUp() throws Exception {
|
|
@@ -23,46 +31,70 @@ public class PersonControllerTest {
|
23
|
31
|
|
24
|
32
|
@Test
|
25
|
33
|
public void createPersonTest(){
|
26
|
|
- when(controller.createPerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.CREATED));
|
|
34
|
+ Person person = new Person("a", "b");
|
|
35
|
+ Person savedPerson = new Person();
|
|
36
|
+ when(repository.save(person)).thenReturn(savedPerson);
|
27
|
37
|
|
28
|
|
- Person p = new Person();
|
29
|
|
- ResponseEntity<Person> actual = controller.createPerson(p);
|
30
|
|
- ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.CREATED);
|
|
38
|
+ ResponseEntity<Person> actual = controller.createPerson(person);
|
31
|
39
|
|
32
|
|
- Assert.assertEquals(expected, actual);
|
|
40
|
+ Assert.assertEquals(actual.getStatusCode(), HttpStatus.CREATED);
|
|
41
|
+ Assert.assertEquals(actual.getBody(), savedPerson);
|
33
|
42
|
}
|
34
|
43
|
|
35
|
44
|
@Test
|
36
|
45
|
public void getPersonTest(){
|
37
|
|
- when(controller.getPerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.OK));
|
|
46
|
+ Person person = new Person();
|
|
47
|
+ person.setId(88L);
|
|
48
|
+ when(repository.findOne(88L)).thenReturn(person);
|
38
|
49
|
|
39
|
|
- Person p = new Person();
|
40
|
|
- p.setId(1L);
|
41
|
|
- ResponseEntity<Person> actual = controller.getPerson(1);
|
42
|
|
- ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.OK);
|
|
50
|
+ ResponseEntity<Person> actual = controller.getPerson(88);
|
43
|
51
|
|
44
|
|
- Assert.assertEquals(expected, actual);
|
|
52
|
+ Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
|
|
53
|
+ Assert.assertEquals(actual.getBody(), person);
|
45
|
54
|
}
|
46
|
55
|
|
47
|
56
|
@Test
|
48
|
|
- public void updatePersonTest(){
|
49
|
|
- when(controller.updatePerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.OK));
|
|
57
|
+ public void getPersonList(){
|
|
58
|
+ Iterable<Person> personList = new ArrayList<>();
|
|
59
|
+ when(repository.findAll()).thenReturn(personList);
|
50
|
60
|
|
51
|
|
- Person p = new Person();
|
52
|
|
- p.setId(1L);
|
53
|
|
- ResponseEntity<Person> actual = controller.updatePerson(any(Person.class));
|
54
|
|
- ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.OK);
|
|
61
|
+ ResponseEntity<Iterable<Person>> actual = controller.getPersonList();
|
55
|
62
|
|
56
|
|
- Assert.assertEquals(expected, actual);
|
|
63
|
+ Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
|
|
64
|
+ Assert.assertEquals(actual.getBody(), personList);
|
57
|
65
|
}
|
58
|
66
|
|
59
|
67
|
@Test
|
60
|
|
- public void deletePerson(){
|
61
|
|
- when(controller.deletePerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.NO_CONTENT));
|
|
68
|
+ public void updatePerson(){
|
|
69
|
+ Person p = new Person("a", "b");
|
|
70
|
+ Person update = new Person("a","b");
|
|
71
|
+ when(repository.save(p)).thenReturn(update);
|
62
|
72
|
|
63
|
|
- ResponseEntity<Person> actual = controller.deletePerson(1);
|
64
|
|
- ResponseEntity<Person> expected = new ResponseEntity<>( HttpStatus.NO_CONTENT);
|
|
73
|
+ ResponseEntity<Person> actual = controller.updatePerson(p);
|
65
|
74
|
|
66
|
|
- Assert.assertEquals(expected, actual);
|
|
75
|
+ Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
|
|
76
|
+ Assert.assertEquals(actual.getBody(), update);
|
67
|
77
|
}
|
|
78
|
+
|
|
79
|
+// @Test
|
|
80
|
+// public void updatePersonTest(){
|
|
81
|
+// when(controller.updatePerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.OK));
|
|
82
|
+//
|
|
83
|
+// Person p = new Person();
|
|
84
|
+// p.setId(1L);
|
|
85
|
+// ResponseEntity<Person> actual = controller.updatePerson(any(Person.class));
|
|
86
|
+// ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.OK);
|
|
87
|
+//
|
|
88
|
+// Assert.assertEquals(expected, actual);
|
|
89
|
+// }
|
|
90
|
+//
|
|
91
|
+// @Test
|
|
92
|
+// public void deletePerson(){
|
|
93
|
+// when(controller.deletePerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.NO_CONTENT));
|
|
94
|
+//
|
|
95
|
+// ResponseEntity<Person> actual = controller.deletePerson(1);
|
|
96
|
+// ResponseEntity<Person> expected = new ResponseEntity<>( HttpStatus.NO_CONTENT);
|
|
97
|
+//
|
|
98
|
+// Assert.assertEquals(expected, actual);
|
|
99
|
+// }
|
68
|
100
|
}
|