|
|
@@ -8,14 +8,18 @@ import org.mockito.InjectMocks;
|
|
8
|
8
|
import org.mockito.Mock;
|
|
9
|
9
|
import org.mockito.Mockito;
|
|
10
|
10
|
import org.mockito.MockitoAnnotations;
|
|
|
11
|
+import org.springframework.http.HttpStatus;
|
|
|
12
|
+import org.springframework.http.ResponseEntity;
|
|
11
|
13
|
import org.springframework.util.Assert;
|
|
12
|
14
|
|
|
13
|
15
|
import java.util.ArrayList;
|
|
14
|
16
|
import java.util.List;
|
|
15
|
17
|
|
|
16
|
18
|
import static org.junit.Assert.assertEquals;
|
|
|
19
|
+import static org.mockito.ArgumentMatchers.any;
|
|
17
|
20
|
import static org.mockito.Mockito.mock;
|
|
18
|
21
|
import static org.mockito.Mockito.verify;
|
|
|
22
|
+import static org.mockito.Mockito.when;
|
|
19
|
23
|
|
|
20
|
24
|
/**
|
|
21
|
25
|
* project: spring-demo
|
|
|
@@ -37,7 +41,16 @@ public class PollControllerTest {
|
|
37
|
41
|
public void setup() {
|
|
38
|
42
|
MockitoAnnotations.initMocks(this);
|
|
39
|
43
|
|
|
40
|
|
- Mockito.when(pollRepo.findAll()).thenReturn(polls);
|
|
|
44
|
+ // GET all polls
|
|
|
45
|
+ when(pollRepo.findAll()).thenReturn(polls);
|
|
|
46
|
+
|
|
|
47
|
+ //POST create poll
|
|
|
48
|
+ when(pollRepo.save(any(Poll.class))).thenAnswer(inv -> inv.getArgument(0));
|
|
|
49
|
+
|
|
|
50
|
+// would be nice...
|
|
|
51
|
+// when(pollRepo::save)
|
|
|
52
|
+// .onSuccess(HttpStatus.CREATED)
|
|
|
53
|
+// .onFail(HttpStatus.BAD_REQUEST);
|
|
41
|
54
|
}
|
|
42
|
55
|
|
|
43
|
56
|
@Test
|
|
|
@@ -59,4 +72,15 @@ public class PollControllerTest {
|
|
59
|
72
|
verify(pollRepo).findAll();
|
|
60
|
73
|
assertEquals(expected, actual);
|
|
61
|
74
|
}
|
|
|
75
|
+
|
|
|
76
|
+ @Test
|
|
|
77
|
+ public void createPollSavesPoll() {
|
|
|
78
|
+ Poll expected = new Poll();
|
|
|
79
|
+ expected.setId(1L);
|
|
|
80
|
+
|
|
|
81
|
+ Poll actual = pollCtrl.createPoll(expected).getBody();
|
|
|
82
|
+
|
|
|
83
|
+ verify(pollRepo).save(any(Poll.class));
|
|
|
84
|
+ assertEquals(expected.getId(), actual.getId());
|
|
|
85
|
+ }
|
|
62
|
86
|
}
|