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