Przeglądaj źródła

before converting tests to mockito

Trinh Tong 7 lat temu
rodzic
commit
7c4358ae44

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

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

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

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