-
Notifications
You must be signed in to change notification settings - Fork 30
/
InfoControllerIT.java
66 lines (55 loc) · 2.7 KB
/
InfoControllerIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.edwise.springbootseries.integrationtests;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
@WebIntegrationTest({"server.port=0"})
public class InfoControllerIT {
private static final Long INFO_ID_1234 = 1234l;
@Autowired
protected WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders
.webAppContextSetup(this.webApplicationContext)
.build();
}
@Test
public void getInfo_ShouldReturnCorrectInfo() throws Exception {
String jsonExpected = "{\"id\":1234,\"info\":\"Info 1234\",\"creationDateTime\":\"2001-12-12T13:40:30\"}";
mockMvc.perform(get("/api/info/{id}", INFO_ID_1234))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().string(jsonExpected));
}
@Test
public void postInfo_ShouldReturnCreatedStatusAndCorrectInfo() throws Exception {
String jsonExpected = "{\"id\":1234,\"info\":\"Info 1234 New\",\"creationDateTime\":\"2015-10-25T19:13:21\"}";
mockMvc.perform(post("/api/info/")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"info\":\"Info 1234 New\",\"creationDateTime\":\"2015-10-25T19:13:21\"}"))
.andExpect(status().isCreated())
.andExpect(content().string(jsonExpected));
;
}
@Test
public void deleteInfo_ShouldReturnNoContentStatus() throws Exception {
mockMvc.perform(delete("/api/info/{id}", INFO_ID_1234))
.andExpect(status().isNoContent());
}
}