|
@@ -20,52 +20,35 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.
|
20
|
20
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
21
|
21
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
22
|
22
|
|
23
|
|
-import org.junit.Before;
|
24
|
23
|
import org.junit.Test;
|
25
|
24
|
import org.junit.runner.RunWith;
|
26
|
|
-
|
27
|
25
|
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;
|
|
26
|
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
27
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
28
|
+import org.springframework.test.context.junit4.SpringRunner;
|
31
|
29
|
import org.springframework.test.web.servlet.MockMvc;
|
32
|
|
-import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
33
|
|
-import org.springframework.web.context.WebApplicationContext;
|
34
|
30
|
|
35
|
|
-/**
|
36
|
|
- * @author Greg Turnquist
|
37
|
|
- */
|
38
|
|
-@RunWith(SpringJUnit4ClassRunner.class)
|
39
|
|
-@SpringApplicationConfiguration(classes = Application.class)
|
40
|
|
-@WebAppConfiguration
|
|
31
|
+@RunWith(SpringRunner.class)
|
|
32
|
+@SpringBootTest
|
|
33
|
+@AutoConfigureMockMvc
|
41
|
34
|
public class GreetingControllerTests {
|
42
|
35
|
|
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
|
|
- }
|
|
36
|
+ @Autowired
|
|
37
|
+ private MockMvc mockMvc;
|
52
|
38
|
|
53
|
|
- @Test
|
54
|
|
- public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
|
|
39
|
+ @Test
|
|
40
|
+ public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
|
55
|
41
|
|
56
|
|
- this.mockMvc.perform(get("/greeting"))
|
57
|
|
- .andDo(print())
|
58
|
|
- .andExpect(status().isOk())
|
59
|
|
- .andExpect(jsonPath("$.content").value("Hello, World!"));
|
60
|
|
- }
|
|
42
|
+ this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
|
|
43
|
+ .andExpect(jsonPath("$.content").value("Hello, World!"));
|
|
44
|
+ }
|
61
|
45
|
|
62
|
|
- @Test
|
63
|
|
- public void paramGreetingShouldReturnTailoredMessage() throws Exception {
|
|
46
|
+ @Test
|
|
47
|
+ public void paramGreetingShouldReturnTailoredMessage() throws Exception {
|
64
|
48
|
|
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
|
|
- }
|
|
49
|
+ this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
|
|
50
|
+ .andDo(print()).andExpect(status().isOk())
|
|
51
|
+ .andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
|
|
52
|
+ }
|
70
|
53
|
|
71
|
54
|
}
|