Преглед изворни кода

Revert "Revert "before converting tests to mockito""

This reverts commit 4792e6a461ef1795560e0efce2919b7fa7a01035.
Trinh Tong пре 5 година
родитељ
комит
85ea85953f

+ 26
- 10
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java Прегледај датотеку

@@ -17,6 +17,7 @@ import org.springframework.boot.test.json.JacksonTester;
17 17
 import org.springframework.http.HttpHeaders;
18 18
 import org.springframework.http.HttpStatus;
19 19
 import org.springframework.http.MediaType;
20
+import org.springframework.http.ResponseEntity;
20 21
 import org.springframework.mock.web.MockHttpServletResponse;
21 22
 import org.springframework.test.context.junit4.SpringRunner;
22 23
 import org.springframework.test.web.servlet.MockMvc;
@@ -26,8 +27,14 @@ import java.net.URI;
26 27
 import java.util.Arrays;
27 28
 
28 29
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
30
+import static org.hamcrest.Matchers.containsString;
29 31
 import static org.mockito.BDDMockito.given;
32
+import static org.mockito.Mockito.doNothing;
33
+import static org.mockito.Mockito.times;
34
+import static org.mockito.Mockito.verify;
30 35
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
36
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
37
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31 38
 
32 39
 @SpringBootTest
33 40
 @RunWith(SpringRunner.class)
@@ -111,23 +118,32 @@ public class PollControllerTest {
111 118
      postURLtemplate URL */
112 119
     public void testCreatePollSuccess() throws Exception {
113 120
         Poll testPoll2 = new Poll();
114
-        String expected = "http://localhost/polls/2/";
121
+//        String expected = "http://localhost/polls/2/";
115 122
 
116
-        HttpHeaders responseHeader =  new HttpHeaders();
117
-        responseHeader.setLocation(URI.create(expected));
123
+//        HttpHeaders responseHeader =  new HttpHeaders();
124
+//        responseHeader.setLocation(URI.create(expected));
118 125
 
119 126
         given(pollController.createPoll(testPoll2)).willReturn(null);
127
+//        doNothing().when(pollController).createPoll(testPoll2);
120 128
 
121
-        MockHttpServletResponse response = mvc.perform(post("/polls/2")
122
-                .content(asJsonString(testPoll2))
129
+//        MockHttpServletResponse response = mvc.perform(post("/polls/2")
130
+//                .content(asJsonString(testPoll2))
131
+//                .contentType(MediaType.APPLICATION_JSON)
132
+//                .accept(MediaType.APPLICATION_JSON))
133
+//                .andReturn().getResponse();
134
+
135
+        mvc.perform(post("/polls/{id}",2)
123 136
                 .contentType(MediaType.APPLICATION_JSON)
124
-                .accept(MediaType.APPLICATION_JSON))
125
-                .andReturn().getResponse();
137
+                .contentType(asJsonString(testPoll2)))
138
+                .andExpect(status().isCreated())
139
+                .andExpect(header().string("location", containsString("/polls/2")));
126 140
 
127
-        String header = response.getHeader("Location");
141
+        verify(pollController, times(1)).createPoll(testPoll2);
128 142
 
129
-        Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
130
-        Assert.assertEquals(expected, header);
143
+//        String header = response.getHeader("Location");
144
+//
145
+//        Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
146
+//        Assert.assertEquals(expected, header);
131 147
     }
132 148
 
133 149
     @Test

+ 31
- 19
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java Прегледај датотеку

@@ -36,7 +36,10 @@ import java.util.List;
36 36
 
37 37
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
38 38
 import static org.mockito.BDDMockito.given;
39
+import static org.mockito.Mockito.*;
39 40
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
41
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
42
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40 43
 
41 44
 //@SpringBootTest
42 45
 @RunWith(SpringRunner.class)
@@ -97,21 +100,6 @@ public class VoteControllerTest {
97 100
         Assert.assertEquals(response.getContentAsString(), votes.toString());
98 101
     }
99 102
 
100
-//    @Test(expected = ResourceNotFoundException.class)
101
-    @Test
102
-    public void testGetVoteIdFail() throws Exception {
103
-        given(voteRepository.findVotesByPoll(BAD_ID))
104
-                .willThrow(new ResourceNotFoundException());
105
-
106
-        MockHttpServletResponse response = mvc.perform(get("/polls/500/votes")
107
-                .accept(MediaType.APPLICATION_JSON))
108
-                .andReturn().getResponse();
109
-
110
-        System.out.println(response.getStatus());
111
-
112
-        Assert.assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatus());
113
-    }
114
-
115 103
     @Test
116 104
     public void testGetAllVotes() throws Exception {
117 105
         given(voteController.getAllVotes())
@@ -132,15 +120,39 @@ public class VoteControllerTest {
132 120
 
133 121
         MockHttpServletResponse response = mvc.perform(
134 122
                 get("polls/votes")
135
-                        .accept(MediaType.APPLICATION_JSON))
136
-                .andReturn().getResponse();
123
+                    .accept(MediaType.APPLICATION_JSON))
124
+                    .andReturn().getResponse();
137 125
 
138 126
         Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
139 127
         Assert.assertTrue(response.getContentAsString().isEmpty());
140 128
     }
141 129
 
142 130
     @Test
143
-    public void testCreateVoteSuccess() {
131
+    public void testCreateVoteSuccess() throws Exception {
132
+        Vote testVote2 = new Vote();
133
+        Long pollId = 2L;
134
+        given(voteController.createVote(pollId, testVote2)).willReturn(null);
144 135
 
136
+        mvc.perform(post("/polls/{id}/votes",pollId)
137
+                .contentType(MediaType.APPLICATION_JSON)
138
+                .contentType(asJsonString(testVote2)))
139
+                .andExpect(status().isCreated());
140
+
141
+        verify(voteController, times(1)).createVote(pollId, testVote2);
142
+    }
143
+//
144
+//    @Test
145
+//    public void test_create_user_fail_404_not_found() throws Exception {
146
+//        User user = new User("username exists");
147
+//
148
+//        when(userService.exists(user)).thenReturn(true);
149
+//
150
+//        mockMvc.perform(
151
+//                post("/users")
152
+//                        .contentType(MediaType.APPLICATION_JSON)
153
+//                        .content(asJsonString(user)))
154
+//                .andExpect(status().isConflict());
155
+//
156
+//        verify(userService, times(1)).exists(user);
157
+//        verifyNoMoreInteractions(userService);
145 158
     }
146
-}