Trinh Tong 5 vuotta sitten
vanhempi
commit
9f3fc5017d

+ 0
- 7
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java Näytä tiedosto

@@ -5,27 +5,20 @@ 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;
9 8
 import org.junit.Assert;
10 9
 import org.junit.Before;
11 10
 import org.junit.Test;
12 11
 import org.junit.runner.RunWith;
13 12
 import org.mockito.InjectMocks;
14
-import org.mockito.Mock;
15 13
 import org.springframework.beans.factory.annotation.Autowired;
16
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
17 14
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
18
-import org.springframework.boot.test.context.SpringBootTest;
19 15
 import org.springframework.boot.test.json.JacksonTester;
20 16
 import org.springframework.boot.test.mock.mockito.MockBean;
21
-import org.springframework.http.HttpHeaders;
22 17
 import org.springframework.http.HttpStatus;
23 18
 import org.springframework.http.MediaType;
24
-import org.springframework.http.ResponseEntity;
25 19
 import org.springframework.mock.web.MockHttpServletResponse;
26 20
 import org.springframework.test.context.junit4.SpringRunner;
27 21
 import org.springframework.test.web.servlet.MockMvc;
28
-import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
29 22
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
30 23
 
31 24
 import java.util.Arrays;

+ 25
- 10
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java Näytä tiedosto

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3 3
 import com.fasterxml.jackson.databind.ObjectMapper;
4
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4 5
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
5 6
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
6 7
 import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
@@ -15,21 +16,21 @@ import org.springframework.beans.factory.annotation.Autowired;
15 16
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
16 17
 import org.springframework.boot.test.json.JacksonTester;
17 18
 import org.springframework.boot.test.mock.mockito.MockBean;
18
-import org.springframework.http.HttpHeaders;
19 19
 import org.springframework.http.HttpStatus;
20 20
 import org.springframework.http.MediaType;
21 21
 import org.springframework.mock.web.MockHttpServletResponse;
22 22
 import org.springframework.test.context.junit4.SpringRunner;
23 23
 import org.springframework.test.web.servlet.MockMvc;
24 24
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
25
-import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
26 25
 
27 26
 import java.util.Arrays;
27
+import java.util.HashSet;
28 28
 
29 29
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
30 30
 import static org.mockito.BDDMockito.given;
31 31
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
32 32
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
33
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
33 34
 
34 35
 @RunWith(SpringRunner.class)
35 36
 @WebMvcTest(VoteController.class)
@@ -41,7 +42,8 @@ public class VoteControllerTest {
41 42
     private Vote testVote;
42 43
     private Iterable<Vote> votes;
43 44
     private final Long TEST_ID = 1L;
44
-    private final Long BAD_ID = 400L;
45
+    private Option option1, option2;
46
+    private HashSet<Option> options1;
45 47
 
46 48
     @MockBean
47 49
     private VoteRepository voteRepository;
@@ -73,6 +75,20 @@ public class VoteControllerTest {
73 75
 
74 76
         testPoll = new Poll();
75 77
         testPoll.setId(TEST_ID);
78
+        options1 = new HashSet<>();
79
+
80
+        option1 = new Option();
81
+        option1.setValue("Yes");
82
+        option1.setId(1L);
83
+
84
+        option2 = new Option();
85
+        option2.setValue("No");
86
+        option2.setId(2L);
87
+
88
+        options1.add(option1);
89
+        options1.add(option2);
90
+
91
+        testPoll.setOptions(options1);
76 92
     }
77 93
 
78 94
     @Test
@@ -121,16 +137,15 @@ public class VoteControllerTest {
121 137
     @Test
122 138
     public void testCreateVoteSuccess() throws Exception {
123 139
         Vote testVote2 = new Vote();
140
+        testVote2.setId(TEST_ID);
124 141
 
125
-        given(voteRepository.save(testVote2))
126
-                .willReturn(testVote2);
142
+        given(pollRepository.findOne(TEST_ID)).willReturn(testPoll);
143
+        given(voteRepository.save(testVote2)).willReturn(testVote2);
127 144
 
128
-        MockHttpServletResponse response = mvc.perform(post("/polls/2/votes")
145
+        mvc.perform(post("/polls/1/votes/1")
146
+                .content(asJsonString(testVote2))
129 147
                 .contentType(MediaType.APPLICATION_JSON)
130 148
                 .accept(MediaType.APPLICATION_JSON))
131
-                .andReturn().getResponse();
132
-
133
-
134
-        Assert.assertEquals(response.getStatus(), HttpStatus.CREATED.value());
149
+                .andExpect(status().isCreated());
135 150
     }
136 151
 }