Skip to content

Commit 93bdba0

Browse files
committed
Spring Boot 2.x基础教程:创建基础项目
1 parent 59b86f4 commit 93bdba0

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.didispace.chapter11;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class HelloController {
8+
9+
@RequestMapping("/hello")
10+
public String index() {
11+
return "Hello World";
12+
}
13+
14+
}
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
package com.didispace.chapter11;
22

3+
import org.junit.Before;
34
import org.junit.Test;
45
import org.junit.runner.RunWith;
56
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.http.MediaType;
68
import org.springframework.test.context.junit4.SpringRunner;
9+
import org.springframework.test.web.servlet.MockMvc;
10+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
11+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
12+
13+
import static org.hamcrest.Matchers.equalTo;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
16+
717

818
@RunWith(SpringRunner.class)
919
@SpringBootTest
1020
public class Chapter11ApplicationTests {
1121

12-
@Test
13-
public void contextLoads() {
14-
}
22+
private MockMvc mvc;
23+
24+
@Before
25+
public void setUp() throws Exception {
26+
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
27+
}
28+
29+
@Test
30+
public void getHello() throws Exception {
31+
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
32+
.andExpect(status().isOk())
33+
.andExpect(content().string(equalTo("Hello World")));
34+
}
1535

1636
}

0 commit comments

Comments
 (0)