|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+/*
|
|
|
2
|
+ * (c) Copyright 2016 大头蚁
|
|
|
3
|
+ * https://github.com/yiifaa
|
|
|
4
|
+ */
|
|
|
5
|
+package com.yiifaa;
|
|
|
6
|
+
|
|
|
7
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
8
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
|
|
9
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
10
|
+
|
|
|
11
|
+import org.junit.Before;
|
|
|
12
|
+import org.junit.Test;
|
|
|
13
|
+import org.junit.runner.RunWith;
|
|
|
14
|
+import org.springframework.boot.test.SpringApplicationConfiguration;
|
|
|
15
|
+import org.springframework.http.MediaType;
|
|
|
16
|
+import org.springframework.mock.web.MockServletContext;
|
|
|
17
|
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
|
18
|
+import org.springframework.test.context.web.WebAppConfiguration;
|
|
|
19
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
20
|
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
|
|
21
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
22
|
+
|
|
|
23
|
+import hello.HelloController;
|
|
|
24
|
+
|
|
|
25
|
+/**
|
|
|
26
|
+ *
|
|
|
27
|
+ * @author <a href="mailto:yiifaa@163.com>大头蚁</a>
|
|
|
28
|
+ * since 1.0
|
|
|
29
|
+ * 2016年5月12日 下午5:45:23
|
|
|
30
|
+ */
|
|
|
31
|
+
|
|
|
32
|
+@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
33
|
+@SpringApplicationConfiguration(classes = MockServletContext.class)
|
|
|
34
|
+@WebAppConfiguration
|
|
|
35
|
+public class HelloControllerTest {
|
|
|
36
|
+
|
|
|
37
|
+ private MockMvc mvc;
|
|
|
38
|
+
|
|
|
39
|
+ @Before
|
|
|
40
|
+ public void setUp() throws Exception {
|
|
|
41
|
+ mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ @Test
|
|
|
45
|
+ public void getHello() throws Exception {
|
|
|
46
|
+ mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
|
|
|
47
|
+ .andExpect(status().isOk())
|
|
|
48
|
+ .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+}
|