Browse Source

improved testing, all tests pass

mpierse 5 years ago
parent
commit
ff128d9a0e
1 changed files with 29 additions and 24 deletions
  1. 29
    24
      src/test/java/io/zipcoder/crudapp/PersonControllerTest.java

+ 29
- 24
src/test/java/io/zipcoder/crudapp/PersonControllerTest.java View File

@@ -6,12 +6,15 @@ import org.junit.Test;
6 6
 import org.mockito.InjectMocks;
7 7
 import org.mockito.Mock;
8 8
 import org.mockito.MockitoAnnotations;
9
+import org.mockito.invocation.InvocationOnMock;
10
+import org.mockito.stubbing.Answer;
9 11
 import org.springframework.http.HttpStatus;
10 12
 import org.springframework.http.ResponseEntity;
11 13
 
12 14
 import java.util.ArrayList;
13 15
 
14 16
 import static org.mockito.Matchers.any;
17
+import static org.mockito.Mockito.doAnswer;
15 18
 import static org.mockito.Mockito.when;
16 19
 
17 20
 public class PersonControllerTest {
@@ -54,7 +57,7 @@ public class PersonControllerTest {
54 57
     }
55 58
 
56 59
     @Test
57
-    public void getPersonList(){
60
+    public void getPersonListTest(){
58 61
         Iterable<Person> personList = new ArrayList<>();
59 62
         when(repository.findAll()).thenReturn(personList);
60 63
 
@@ -65,7 +68,7 @@ public class PersonControllerTest {
65 68
     }
66 69
 
67 70
     @Test
68
-    public void updatePerson(){
71
+    public void updatePersonTest(){
69 72
         Person p = new Person("a", "b");
70 73
         Person update = new Person("a","b");
71 74
         when(repository.save(p)).thenReturn(update);
@@ -75,26 +78,28 @@ public class PersonControllerTest {
75 78
         Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
76 79
         Assert.assertEquals(actual.getBody(), update);
77 80
     }
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
-//    }
81
+
82
+    @Test
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);
99
+
100
+        ResponseEntity<Person> actual = controller.deletePerson(8783);
101
+
102
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.NO_CONTENT);
103
+        Assert.assertEquals(actual.getBody(), null);
104
+    }
100 105
 }