|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+package io.zipcoder.tc_spring_poll_application.controllers;
|
|
|
2
|
+
|
|
|
3
|
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
|
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
|
|
|
5
|
+import org.junit.Before;
|
|
|
6
|
+import org.junit.Test;
|
|
|
7
|
+import org.mockito.InjectMocks;
|
|
|
8
|
+import org.mockito.Mock;
|
|
|
9
|
+import org.mockito.Mockito;
|
|
|
10
|
+import org.mockito.MockitoAnnotations;
|
|
|
11
|
+import org.springframework.util.Assert;
|
|
|
12
|
+
|
|
|
13
|
+import java.util.ArrayList;
|
|
|
14
|
+import java.util.List;
|
|
|
15
|
+
|
|
|
16
|
+import static org.junit.Assert.assertEquals;
|
|
|
17
|
+import static org.mockito.Mockito.mock;
|
|
|
18
|
+import static org.mockito.Mockito.verify;
|
|
|
19
|
+
|
|
|
20
|
+/**
|
|
|
21
|
+ * project: spring-demo
|
|
|
22
|
+ * package: io.zipcoder.tc_spring_poll_application.controllers
|
|
|
23
|
+ * author: https://github.com/vvmk
|
|
|
24
|
+ * date: 4/5/18
|
|
|
25
|
+ */
|
|
|
26
|
+public class PollControllerTest {
|
|
|
27
|
+
|
|
|
28
|
+ @InjectMocks
|
|
|
29
|
+ private PollController pollCtrl;
|
|
|
30
|
+
|
|
|
31
|
+ @Mock
|
|
|
32
|
+ private PollRepository pollRepo;
|
|
|
33
|
+
|
|
|
34
|
+ private List<Poll> polls = new ArrayList<>();
|
|
|
35
|
+
|
|
|
36
|
+ @Before
|
|
|
37
|
+ public void setup() {
|
|
|
38
|
+ MockitoAnnotations.initMocks(this);
|
|
|
39
|
+
|
|
|
40
|
+ Mockito.when(pollRepo.findAll()).thenReturn(polls);
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+ @Test
|
|
|
44
|
+ public void getAllPollsReturnsIterable() {
|
|
|
45
|
+ Class<?> expected = Iterable.class;
|
|
|
46
|
+ Class<?> actual = pollCtrl.getAllPolls().getBody().getClass();
|
|
|
47
|
+
|
|
|
48
|
+ verify(pollRepo).findAll();
|
|
|
49
|
+ Assert.isAssignable(expected, actual);
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ @Test
|
|
|
53
|
+ public void getAllPollsReturnsPolls() {
|
|
|
54
|
+ polls.add(mock(Poll.class));
|
|
|
55
|
+
|
|
|
56
|
+ long expected = 1;
|
|
|
57
|
+ long actual = pollCtrl.getAllPolls().getBody().spliterator().getExactSizeIfKnown();
|
|
|
58
|
+
|
|
|
59
|
+ verify(pollRepo).findAll();
|
|
|
60
|
+ assertEquals(expected, actual);
|
|
|
61
|
+ }
|
|
|
62
|
+}
|