|
@@ -0,0 +1,38 @@
|
|
1
|
+package hello;
|
|
2
|
+
|
|
3
|
+import org.junit.Before;
|
|
4
|
+import org.junit.Test;
|
|
5
|
+import org.junit.runner.RunWith;
|
|
6
|
+import org.springframework.boot.test.SpringApplicationConfiguration;
|
|
7
|
+import org.springframework.http.MediaType;
|
|
8
|
+import org.springframework.mock.web.MockServletContext;
|
|
9
|
+import org.springframework.test.context.ContextConfiguration;
|
|
10
|
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
11
|
+import org.springframework.test.context.web.WebAppConfiguration;
|
|
12
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
13
|
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
|
14
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
15
|
+
|
|
16
|
+import static org.hamcrest.Matchers.is;
|
|
17
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
|
18
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
19
|
+
|
|
20
|
+@RunWith(SpringJUnit4ClassRunner.class)
|
|
21
|
+@SpringApplicationConfiguration(classes = Application.class)
|
|
22
|
+@ContextConfiguration(classes = MockServletContext.class)
|
|
23
|
+@WebAppConfiguration
|
|
24
|
+public class HelloControllerTest {
|
|
25
|
+ private MockMvc mvc;
|
|
26
|
+
|
|
27
|
+ @Before
|
|
28
|
+ public void setUp() throws Exception {
|
|
29
|
+ mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ @Test
|
|
33
|
+ public void getHello() throws Exception {
|
|
34
|
+ mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
|
|
35
|
+ .andExpect(status().isOk())
|
|
36
|
+ .andExpect(content().string(is("Greetings from Spring Boot!")));
|
|
37
|
+ }
|
|
38
|
+}
|