Browse Source

updating tests

Trinh Tong 5 years ago
parent
commit
b5d35833fd

+ 36
- 18
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java View File

@@ -5,6 +5,7 @@ import io.zipcoder.tc_spring_poll_application.domain.Option;
5 5
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
6 6
 import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
7 7
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
8
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
8 9
 import org.junit.Assert;
9 10
 import org.junit.Before;
10 11
 import org.junit.Test;
@@ -13,8 +14,10 @@ import org.mockito.InjectMocks;
13 14
 import org.mockito.Mock;
14 15
 import org.springframework.beans.factory.annotation.Autowired;
15 16
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
17
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
16 18
 import org.springframework.boot.test.context.SpringBootTest;
17 19
 import org.springframework.boot.test.json.JacksonTester;
20
+import org.springframework.boot.test.mock.mockito.MockBean;
18 21
 import org.springframework.http.HttpHeaders;
19 22
 import org.springframework.http.HttpStatus;
20 23
 import org.springframework.http.MediaType;
@@ -22,6 +25,7 @@ import org.springframework.http.ResponseEntity;
22 25
 import org.springframework.mock.web.MockHttpServletResponse;
23 26
 import org.springframework.test.context.junit4.SpringRunner;
24 27
 import org.springframework.test.web.servlet.MockMvc;
28
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
25 29
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
26 30
 
27 31
 import java.util.Arrays;
@@ -31,25 +35,29 @@ import java.util.Set;
31 35
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
32 36
 import static org.mockito.BDDMockito.given;
33 37
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
38
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34 39
 
35 40
 
36
-@SpringBootTest
37 41
 @RunWith(SpringRunner.class)
38
-@AutoConfigureMockMvc
42
+@WebMvcTest(PollController.class)
39 43
 public class PollControllerTest {
44
+
40 45
     @Autowired
41 46
     private MockMvc mvc;
47
+
48
+    // ==== Instance variables ==== //
42 49
     private Poll testPoll, testPoll2;
43 50
     private Iterable<Poll> polls;
44 51
     private final Long POLL_ID = 1L;
52
+    private final Long BAD_ID = 1100L;
45 53
     private Set<Option> options1, options2;
46 54
     private Option option1, option2, option3, option4;
47 55
 
48
-
49
-    @Mock
56
+    @MockBean
50 57
     private PollRepository pollRepository;
51 58
 
52 59
     @InjectMocks
60
+    @Autowired
53 61
     private PollController pollController;
54 62
 
55 63
     @Before
@@ -102,6 +110,8 @@ public class PollControllerTest {
102 110
         polls = Arrays.asList(pollArr);
103 111
     }
104 112
 
113
+    // =================== Get one =================== //
114
+
105 115
     @Test
106 116
     public void testGetPollByIdSuccess() throws Exception {
107 117
 
@@ -109,12 +119,15 @@ public class PollControllerTest {
109 119
                 .willReturn(testPoll);
110 120
 
111 121
         MockHttpServletResponse response = mvc.perform(get("/polls/1")
122
+                .content(asJsonString(testPoll))
112 123
                 .accept(MediaType.APPLICATION_JSON))
113 124
                 .andReturn().getResponse();
114 125
 
115 126
         Assert.assertEquals(HttpStatus.OK.value(), (response.getStatus()));
116 127
     }
117 128
 
129
+    // =================== Get All =================== //
130
+
118 131
     @Test
119 132
     public void testGetAllPollsSuccess() throws Exception {
120 133
 
@@ -143,9 +156,11 @@ public class PollControllerTest {
143 156
         Assert.assertTrue(response.getContentAsString().isEmpty());
144 157
     }
145 158
 
159
+    // =================== Create Poll =================== //
160
+
146 161
     @Test
147 162
     public void testCreatePollSuccess() throws Exception {
148
-        given(pollController.createPoll(testPoll2)).willReturn(null);
163
+        given(pollRepository.save(testPoll2)).willReturn(testPoll2);
149 164
 
150 165
         MockHttpServletResponse response = mvc.perform(post("/polls/")
151 166
                 .content(asJsonString(testPoll2))
@@ -156,16 +171,18 @@ public class PollControllerTest {
156 171
         Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
157 172
     }
158 173
 
174
+    // =================== Update Poll =================== //
175
+
159 176
     @Test
160 177
     public void testUpdatePollSuccess() {
161 178
 
162 179
         /* can't get it to work, rewriting */
163 180
     }
164 181
 
165
-    @Test(expected = ResourceNotFoundException.class)
182
+    @Test
166 183
     public void testUpdatePollFail() throws Exception {
167
-        given(pollController.updatePoll(testPoll, 100L))
168
-                .willThrow(new ResourceNotFoundException());
184
+
185
+        given(pollRepository.findOne(BAD_ID)).willReturn(null);
169 186
 
170 187
         MockHttpServletResponse response = mvc.perform(put("/polls/10101")
171 188
                 .content(asJsonString(testPoll))
@@ -176,23 +193,21 @@ public class PollControllerTest {
176 193
         Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
177 194
     }
178 195
 
196
+    // =================== Delete Poll =================== //
197
+
179 198
     @Test
180 199
     public void testDeletePollSuccess() throws Exception {
181
-        given(pollController.deletePoll(testPoll.getId()))
182
-                .willReturn(null);
200
+        given(pollRepository.findOne(1l))
201
+                .willReturn(new Poll());
183 202
 
184
-        MockHttpServletResponse response = mvc.perform(delete("/polls")
185
-                .content(asJsonString(testPoll))
186
-                .contentType(MediaType.APPLICATION_JSON)
203
+        mvc.perform(delete("/polls/1")
187 204
                 .accept(MediaType.APPLICATION_JSON))
188
-                .andReturn().getResponse();
189
-
190
-        Assert.assertEquals(HttpStatus.OK.value(),(response.getStatus()));
205
+                .andExpect(status().isOk());
191 206
     }
192 207
 
193
-    @Test(expected = ResourceNotFoundException.class)
208
+    @Test
194 209
     public void testDeletePollFail() throws Exception {
195
-        given(pollController.deletePoll(400L))
210
+        given(pollRepository.findOne(BAD_ID))
196 211
                 .willThrow(new ResourceNotFoundException());
197 212
 
198 213
         MockHttpServletResponse response = mvc.perform(delete("/polls/400")
@@ -202,4 +217,7 @@ public class PollControllerTest {
202 217
         Assert.assertEquals(HttpStatus.NOT_FOUND.value(),(response.getStatus()));
203 218
 
204 219
     }
220
+
221
+
222
+    // =================== Test Exception Thrown =================== //
205 223
 }

+ 10
- 20
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java View File

@@ -11,17 +11,13 @@ import org.junit.Before;
11 11
 import org.junit.Test;
12 12
 import org.junit.runner.RunWith;
13 13
 import org.mockito.InjectMocks;
14
-import org.mockito.Mock;
15 14
 import org.springframework.beans.factory.annotation.Autowired;
16
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
17 15
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
18
-import org.springframework.boot.test.context.SpringBootTest;
19 16
 import org.springframework.boot.test.json.JacksonTester;
20 17
 import org.springframework.boot.test.mock.mockito.MockBean;
21 18
 import org.springframework.http.HttpHeaders;
22 19
 import org.springframework.http.HttpStatus;
23 20
 import org.springframework.http.MediaType;
24
-import org.springframework.http.ResponseEntity;
25 21
 import org.springframework.mock.web.MockHttpServletResponse;
26 22
 import org.springframework.test.context.junit4.SpringRunner;
27 23
 import org.springframework.test.web.servlet.MockMvc;
@@ -32,15 +28,11 @@ import java.util.Arrays;
32 28
 
33 29
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
34 30
 import static org.mockito.BDDMockito.given;
35
-import static org.mockito.Mockito.times;
36
-import static org.mockito.Mockito.verify;
37 31
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
38 32
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
39
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40 33
 
41
-@SpringBootTest
42 34
 @RunWith(SpringRunner.class)
43
-@AutoConfigureMockMvc
35
+@WebMvcTest(VoteController.class)
44 36
 public class VoteControllerTest {
45 37
 
46 38
     @Autowired
@@ -51,13 +43,14 @@ public class VoteControllerTest {
51 43
     private final Long TEST_ID = 1L;
52 44
     private final Long BAD_ID = 400L;
53 45
 
54
-    @Mock
46
+    @MockBean
55 47
     private VoteRepository voteRepository;
56 48
 
57
-    @Mock
49
+    @MockBean
58 50
     private PollRepository pollRepository;
59 51
 
60 52
     @InjectMocks
53
+    @Autowired
61 54
     private VoteController voteController;
62 55
 
63 56
     private Poll testPoll;
@@ -100,7 +93,7 @@ public class VoteControllerTest {
100 93
 
101 94
     @Test
102 95
     public void testGetAllVotes() throws Exception {
103
-        given(voteController.getAllVotes())
96
+        given(voteRepository.findAll())
104 97
                 .willReturn(votes);
105 98
 
106 99
         MockHttpServletResponse response = mvc.perform(get("/polls/votes")
@@ -113,11 +106,11 @@ public class VoteControllerTest {
113 106
 
114 107
     @Test
115 108
     public void testGetAllVotesFail() throws Exception {
116
-        given(voteController.getAllVotes())
109
+        given(voteRepository.findAll())
117 110
                 .willThrow(new ResourceNotFoundException());
118 111
 
119 112
         MockHttpServletResponse response = mvc.perform(
120
-                get("polls/votes")
113
+                get("polls/votesBADBAD")
121 114
                     .accept(MediaType.APPLICATION_JSON))
122 115
                     .andReturn().getResponse();
123 116
 
@@ -128,12 +121,9 @@ public class VoteControllerTest {
128 121
     @Test
129 122
     public void testCreateVoteSuccess() throws Exception {
130 123
         Vote testVote2 = new Vote();
131
-        Long pollId = 2L;
132
-        HttpHeaders responseHeaders = new HttpHeaders();
133
-        responseHeaders.setLocation(ServletUriComponentsBuilder.
134
-                fromCurrentRequest().path("/{id}").buildAndExpand(testVote2.getId()).toUri());
135
-        given(voteController.createVote(pollId, testVote2))
136
-                .willReturn(new ResponseEntity<>(null, responseHeaders ,HttpStatus.CREATED));
124
+
125
+        given(voteRepository.save(testVote2))
126
+                .willReturn(testVote2);
137 127
 
138 128
         MockHttpServletResponse response = mvc.perform(post("/polls/2/votes")
139 129
                 .contentType(MediaType.APPLICATION_JSON)