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