This repository was archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebLayerTest.java
99 lines (90 loc) · 4.16 KB
/
WebLayerTest.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
package hello;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put;
import static org.springframework.restdocs.request.RequestDocumentation.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(HomeController.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class WebLayerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void patchAlsoHasEmptyHttpieOrCurlOutput() throws Exception {
this.mockMvc.perform(
patch("/{name}", "World")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("greeting", "Hello")
)
.andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello World")))
.andDo(document("patch-with-path-variable",
pathParameters(
parameterWithName("name").description("the name")
),
requestParameters(
parameterWithName("greeting").description("the greeting word")
)
));
}
@Test
public void putHasHttpieAndCurlOutput() throws Exception {
this.mockMvc.perform(
put("/{name}", "World")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("greeting", "Hello")
)
.andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello World")))
.andDo(document("put-with-path-variable",
pathParameters(
parameterWithName("name").description("the name")
),
requestParameters(
parameterWithName("greeting").description("the greeting word")
)
));
}
@Test
public void patchWithoutPathVariable() throws Exception {
this.mockMvc.perform(
patch("/tony")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("greeting", "Hello")
)
.andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello Tony")))
.andDo(document("patch-without-path-variable",
requestParameters(
parameterWithName("greeting").description("the greeting word")
)
));
}
@Test
public void putWithoutPathVariable() throws Exception {
this.mockMvc.perform(
put("/tony")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("greeting", "Hello")
)
.andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello Tony")))
.andDo(document("put-without-path-variable",
requestParameters(
parameterWithName("greeting").description("the greeting word")
)
));
}
}