Trinh Tong 5 years ago
parent
commit
80e486e192

+ 37
- 30
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java View File

@@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired;
15 15
 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
16 16
 import org.springframework.boot.test.context.SpringBootTest;
17 17
 import org.springframework.boot.test.json.JacksonTester;
18
-import org.springframework.http.HttpHeaders;
19 18
 import org.springframework.http.HttpStatus;
20 19
 import org.springframework.http.MediaType;
21 20
 import org.springframework.mock.web.MockHttpServletResponse;
@@ -23,9 +22,9 @@ import org.springframework.test.context.junit4.SpringRunner;
23 22
 import org.springframework.test.web.servlet.MockMvc;
24 23
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
25 24
 
26
-import java.net.URI;
25
+import java.util.Arrays;
26
+import java.util.Collections;
27 27
 
28
-import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
29 28
 import static org.mockito.BDDMockito.given;
30 29
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
31 30
 
@@ -33,10 +32,13 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
33 32
 @RunWith(SpringRunner.class)
34 33
 @AutoConfigureMockMvc
35 34
 public class VoteControllerTest {
35
+
36 36
     @Autowired
37 37
     private MockMvc mvc;
38 38
     private Vote testVote;
39 39
     private Iterable<Vote> votes;
40
+    private Long testId;
41
+
40 42
 
41 43
     @Mock
42 44
     private VoteRepository voteRepository;
@@ -51,25 +53,30 @@ public class VoteControllerTest {
51 53
         JacksonTester.initFields(this, new ObjectMapper());
52 54
 
53 55
         mvc = MockMvcBuilders.standaloneSetup(voteController)
54
-                .setControllerAdvice( new ResourceNotFoundException())
56
+                .setControllerAdvice(new ResourceNotFoundException())
55 57
                 .build();
56 58
 
57 59
         testVote = new Vote();
60
+        testId = 1L;
61
+        testVote.setId(1L);
62
+
63
+        Vote[] votearr = {testVote};
64
+        votes = Arrays.asList(votearr);
58 65
 
59 66
     }
60 67
 
61 68
     @Test
62
-    public void testGetVoteIdSuccess() throws Exception {
63
-
64
-        given(voteRepository.findOne(1L))
65
-                .willReturn(testVote);
66
-
67
-        MockHttpServletResponse response = mvc.perform(get("/polls/1")
68
-                .accept(MediaType.APPLICATION_JSON))
69
-                .andReturn().getResponse();
70
-
71
-        Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
72
-        Assert.assertEquals(response.getContentAsString(), testVote.toString());
69
+    public void testGetVotePollIdSuccess() throws Exception {
70
+
71
+//        given(voteRepository.findVotesByPoll(testId))
72
+//                .willReturn(votes);
73
+//
74
+//        MockHttpServletResponse response = mvc.perform(get("polls/1/votes")
75
+//                .accept(MediaType.APPLICATION_JSON))
76
+//                .andReturn().getResponse();
77
+//
78
+//        Assert.assertEquals((response.getStatus()), HttpStatus.OK.value());
79
+//        Assert.assertEquals(response.getContentAsString(), testVote.toString());
73 80
     }
74 81
 
75 82
     @Test
@@ -78,7 +85,7 @@ public class VoteControllerTest {
78 85
         given(voteRepository.findAll())
79 86
                 .willReturn(null);
80 87
 
81
-        MockHttpServletResponse response = mvc.perform(get("/polls")
88
+        MockHttpServletResponse response = mvc.perform(get("polls/200/votes")
82 89
                 .accept(MediaType.APPLICATION_JSON))
83 90
                 .andReturn().getResponse();
84 91
 
@@ -86,20 +93,20 @@ public class VoteControllerTest {
86 93
         Assert.assertEquals(response.getContentAsString(), votes.toString());
87 94
     }
88 95
 
89
-    @Test
90
-    public void testGetAllVotes() throws Exception {
91
-
92
-        given(voteRepository.findOne(200L))
93
-                .willThrow(new ResourceNotFoundException());
94
-
95
-        MockHttpServletResponse response = mvc.perform(
96
-                get("/polls/200")
97
-                        .accept(MediaType.APPLICATION_JSON))
98
-                .andReturn().getResponse();
99
-
100
-        Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
101
-        Assert.assertTrue(response.getContentAsString().isEmpty());
102
-    }
96
+//    @Test
97
+//    public void testGetAllVotes() throws Exception {
98
+//
99
+//        given(voteRepository.findOne(200L))
100
+//                .willThrow(new ResourceNotFoundException());
101
+//
102
+//        MockHttpServletResponse response = mvc.perform(
103
+//                get("polls/votes")
104
+//                        .accept(MediaType.APPLICATION_JSON))
105
+//                .andReturn().getResponse();
106
+//
107
+//        Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
108
+//        Assert.assertTrue(response.getContentAsString().isEmpty());
109
+//    }
103 110
 
104 111
     @Test
105 112
     public void testGetAllVotesFail() {