Przeglądaj źródła

Revert "before converting tests to mockito"

This reverts commit 7c4358ae44d26c4c45247fafed37c6ab961ccbd7.
Trinh Tong 7 lat temu
rodzic
commit
4792e6a461

+ 10
- 26
src/test/java/io/zipcoder/tc_spring_poll_application/controller/PollControllerTest.java Wyświetl plik

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

+ 19
- 31
src/test/java/io/zipcoder/tc_spring_poll_application/controller/VoteControllerTest.java Wyświetl plik

@@ -36,10 +36,7 @@ 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.*;
40 39
 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;
43 40
 
44 41
 //@SpringBootTest
45 42
 @RunWith(SpringRunner.class)
@@ -100,6 +97,21 @@ public class VoteControllerTest {
100 97
         Assert.assertEquals(response.getContentAsString(), votes.toString());
101 98
     }
102 99
 
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
+
103 115
     @Test
104 116
     public void testGetAllVotes() throws Exception {
105 117
         given(voteController.getAllVotes())
@@ -120,39 +132,15 @@ public class VoteControllerTest {
120 132
 
121 133
         MockHttpServletResponse response = mvc.perform(
122 134
                 get("polls/votes")
123
-                    .accept(MediaType.APPLICATION_JSON))
124
-                    .andReturn().getResponse();
135
+                        .accept(MediaType.APPLICATION_JSON))
136
+                .andReturn().getResponse();
125 137
 
126 138
         Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
127 139
         Assert.assertTrue(response.getContentAsString().isEmpty());
128 140
     }
129 141
 
130 142
     @Test
131
-    public void testCreateVoteSuccess() throws Exception {
132
-        Vote testVote2 = new Vote();
133
-        Long pollId = 2L;
134
-        given(voteController.createVote(pollId, testVote2)).willReturn(null);
143
+    public void testCreateVoteSuccess() {
135 144
 
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);
158 145
     }
146
+}