|
@@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
14
|
14
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
15
|
15
|
import org.springframework.boot.test.context.SpringBootTest;
|
16
|
16
|
import org.springframework.boot.test.json.JacksonTester;
|
|
17
|
+import org.springframework.http.HttpHeaders;
|
17
|
18
|
import org.springframework.http.HttpStatus;
|
18
|
19
|
import org.springframework.http.MediaType;
|
19
|
20
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
@@ -21,10 +22,14 @@ import org.springframework.test.context.junit4.SpringRunner;
|
21
|
22
|
import org.springframework.test.web.servlet.MockMvc;
|
22
|
23
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
23
|
24
|
|
|
25
|
+import java.net.URI;
|
24
|
26
|
import java.util.Arrays;
|
25
|
27
|
|
|
28
|
+import static io.zipcoder.tc_spring_poll_application.JsonTestUtilities.asJsonString;
|
26
|
29
|
import static org.mockito.BDDMockito.given;
|
|
30
|
+import static org.mockito.Matchers.endsWith;
|
27
|
31
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
32
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
28
|
33
|
|
29
|
34
|
@SpringBootTest
|
30
|
35
|
@RunWith(SpringRunner.class)
|
|
@@ -103,4 +108,27 @@ public class PollControllerTest {
|
103
|
108
|
Assert.assertEquals(response.getStatus(), HttpStatus.NOT_FOUND.value());
|
104
|
109
|
Assert.assertTrue(response.getContentAsString().isEmpty());
|
105
|
110
|
}
|
|
111
|
+
|
|
112
|
+ @Test /* This one is failing */
|
|
113
|
+ public void testCreatePollSuccess() throws Exception {
|
|
114
|
+ Poll testPoll2 = new Poll();
|
|
115
|
+ String expected = "http://localhost:8080/polls/2";
|
|
116
|
+
|
|
117
|
+ HttpHeaders responseHeader = new HttpHeaders();
|
|
118
|
+ responseHeader.setLocation(URI.create(expected));
|
|
119
|
+
|
|
120
|
+ given(pollController.createPoll(testPoll2)).willReturn(null);
|
|
121
|
+
|
|
122
|
+ MockHttpServletResponse response = mvc.perform(post("/polls")
|
|
123
|
+ .content(asJsonString(testPoll2))
|
|
124
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
125
|
+ .accept(MediaType.APPLICATION_JSON))
|
|
126
|
+ .andReturn().getResponse();
|
|
127
|
+
|
|
128
|
+ String header = response.getHeader("Location");
|
|
129
|
+
|
|
130
|
+ Assert.assertEquals(HttpStatus.CREATED.value(),(response.getStatus()));
|
|
131
|
+ Assert.assertEquals(expected, header);
|
|
132
|
+ }
|
|
133
|
+
|
106
|
134
|
}
|