|
@@ -0,0 +1,71 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright 2016 the original author or authors.
|
|
3
|
+ *
|
|
4
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+ * you may not use this file except in compliance with the License.
|
|
6
|
+ * You may obtain a copy of the License at
|
|
7
|
+ *
|
|
8
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+ *
|
|
10
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
11
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+ * See the License for the specific language governing permissions and
|
|
14
|
+ * limitations under the License.
|
|
15
|
+ */
|
|
16
|
+package hello;
|
|
17
|
+
|
|
18
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
19
|
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
|
20
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
21
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
22
|
+
|
|
23
|
+import org.junit.Before;
|
|
24
|
+import org.junit.Test;
|
|
25
|
+import org.junit.runner.RunWith;
|
|
26
|
+
|
|
27
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
28
|
+import org.springframework.boot.test.SpringApplicationConfiguration;
|
|
29
|
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
30
|
+import org.springframework.test.context.web.WebAppConfiguration;
|
|
31
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
32
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
33
|
+import org.springframework.web.context.WebApplicationContext;
|
|
34
|
+
|
|
35
|
+/**
|
|
36
|
+ * @author Greg Turnquist
|
|
37
|
+ */
|
|
38
|
+@RunWith(SpringJUnit4ClassRunner.class)
|
|
39
|
+@SpringApplicationConfiguration(classes = Application.class)
|
|
40
|
+@WebAppConfiguration
|
|
41
|
+public class GreetingControllerTests {
|
|
42
|
+
|
|
43
|
+ @Autowired
|
|
44
|
+ private WebApplicationContext ctx;
|
|
45
|
+
|
|
46
|
+ private MockMvc mockMvc;
|
|
47
|
+
|
|
48
|
+ @Before
|
|
49
|
+ public void setUp() {
|
|
50
|
+ this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ @Test
|
|
54
|
+ public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
|
|
55
|
+
|
|
56
|
+ this.mockMvc.perform(get("/greeting"))
|
|
57
|
+ .andDo(print())
|
|
58
|
+ .andExpect(status().isOk())
|
|
59
|
+ .andExpect(jsonPath("$.content").value("Hello, World!"));
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ @Test
|
|
63
|
+ public void paramGreetingShouldReturnTailoredMessage() throws Exception {
|
|
64
|
+
|
|
65
|
+ this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
|
|
66
|
+ .andDo(print())
|
|
67
|
+ .andExpect(status().isOk())
|
|
68
|
+ .andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+}
|