This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
/
MappingControllerTests.java
108 lines (90 loc) · 3.85 KB
/
MappingControllerTests.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.springframework.samples.mvc.mapping;
import static org.hamcrest.Matchers.startsWith;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.samples.mvc.AbstractContextControllerTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringJUnit4ClassRunner.class)
public class MappingControllerTests extends AbstractContextControllerTests {
private MockMvc mockMvc;
@Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
}
@Test
public void byPath() throws Exception {
this.mockMvc.perform(get("/mapping/path")).andExpect(content().string("Mapped by path!"));
}
@Test
public void byPathPattern() throws Exception {
this.mockMvc.perform(get("/mapping/path/wildcard"))
.andExpect(content().string("Mapped by path pattern ('/mapping/path/wildcard')"));
}
@Test
public void byMethod() throws Exception {
this.mockMvc.perform(get("/mapping/method"))
.andExpect(content().string("Mapped by path + method"));
}
@Test
public void byParameter() throws Exception {
this.mockMvc.perform(get("/mapping/parameter?foo=bar"))
.andExpect(content().string("Mapped by path + method + presence of query parameter!"));
}
@Test
public void byNotParameter() throws Exception {
this.mockMvc.perform(get("/mapping/parameter"))
.andExpect(content().string("Mapped by path + method + not presence of query parameter!"));
}
@Test
public void byHeader() throws Exception {
this.mockMvc.perform(get("/mapping/header").header("FooHeader", "foo"))
.andExpect(content().string("Mapped by path + method + presence of header!"));
}
@Test
public void byHeaderNegation() throws Exception {
this.mockMvc.perform(get("/mapping/header"))
.andExpect(content().string("Mapped by path + method + absence of header!"));
}
@Test
public void byConsumes() throws Exception {
this.mockMvc.perform(
post("/mapping/consumes")
.contentType(MediaType.APPLICATION_JSON)
.content("{ \"foo\": \"bar\", \"fruit\": \"apple\" }".getBytes()))
.andExpect(content().string(startsWith("Mapped by path + method + consumable media type (javaBean")));
}
@Test
public void byProducesAcceptJson() throws Exception {
this.mockMvc.perform(get("/mapping/produces").accept(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.foo").value("bar"))
.andExpect(jsonPath("$.fruit").value("apple"));
}
@Test
public void byProducesAcceptXml() throws Exception {
this.mockMvc.perform(get("/mapping/produces").accept(MediaType.APPLICATION_XML))
.andExpect(xpath("/javaBean/foo").string("bar"))
.andExpect(xpath("/javaBean/fruit").string("apple"));
}
@Test
public void byProducesJsonExtension() throws Exception {
this.mockMvc.perform(get("/mapping/produces.json"))
.andExpect(jsonPath("$.foo").value("bar"))
.andExpect(jsonPath("$.fruit").value("apple"));
}
@Test
public void byProducesXmlExtension() throws Exception {
this.mockMvc.perform(get("/mapping/produces.xml"))
.andExpect(xpath("/javaBean/foo").string("bar"))
.andExpect(xpath("/javaBean/fruit").string("apple"));
}
}