Selaa lähdekoodia

Revert "before converting tests to mockito"

This reverts commit 7c4358ae44d26c4c45247fafed37c6ab961ccbd7.
Trinh Tong 7 vuotta sitten
vanhempi
commit
4792e6a461

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

17
 import org.springframework.http.HttpHeaders;
17
 import org.springframework.http.HttpHeaders;
18
 import org.springframework.http.HttpStatus;
18
 import org.springframework.http.HttpStatus;
19
 import org.springframework.http.MediaType;
19
 import org.springframework.http.MediaType;
20
-import org.springframework.http.ResponseEntity;
21
 import org.springframework.mock.web.MockHttpServletResponse;
20
 import org.springframework.mock.web.MockHttpServletResponse;
22
 import org.springframework.test.context.junit4.SpringRunner;
21
 import org.springframework.test.context.junit4.SpringRunner;
23
 import org.springframework.test.web.servlet.MockMvc;
22
 import org.springframework.test.web.servlet.MockMvc;
27
 import java.util.Arrays;
26
 import java.util.Arrays;
28
 
27
 
29
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
28
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
30
-import static org.hamcrest.Matchers.containsString;
31
 import static org.mockito.BDDMockito.given;
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
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
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
 @SpringBootTest
32
 @SpringBootTest
40
 @RunWith(SpringRunner.class)
33
 @RunWith(SpringRunner.class)
118
      postURLtemplate URL */
111
      postURLtemplate URL */
119
     public void testCreatePollSuccess() throws Exception {
112
     public void testCreatePollSuccess() throws Exception {
120
         Poll testPoll2 = new Poll();
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
         given(pollController.createPoll(testPoll2)).willReturn(null);
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
                 .contentType(MediaType.APPLICATION_JSON)
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
     @Test
133
     @Test

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

36
 
36
 
37
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
37
 import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
38
 import static org.mockito.BDDMockito.given;
38
 import static org.mockito.BDDMockito.given;
39
-import static org.mockito.Mockito.*;
40
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
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
 //@SpringBootTest
41
 //@SpringBootTest
45
 @RunWith(SpringRunner.class)
42
 @RunWith(SpringRunner.class)
100
         Assert.assertEquals(response.getContentAsString(), votes.toString());
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
     @Test
115
     @Test
104
     public void testGetAllVotes() throws Exception {
116
     public void testGetAllVotes() throws Exception {
105
         given(voteController.getAllVotes())
117
         given(voteController.getAllVotes())
120
 
132
 
121
         MockHttpServletResponse response = mvc.perform(
133
         MockHttpServletResponse response = mvc.perform(
122
                 get("polls/votes")
134
                 get("polls/votes")
123
-                    .accept(MediaType.APPLICATION_JSON))
124
-                    .andReturn().getResponse();
135
+                        .accept(MediaType.APPLICATION_JSON))
136
+                .andReturn().getResponse();
125
 
137
 
126
         Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
138
         Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
127
         Assert.assertTrue(response.getContentAsString().isEmpty());
139
         Assert.assertTrue(response.getContentAsString().isEmpty());
128
     }
140
     }
129
 
141
 
130
     @Test
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
+}