Skip to content

Commit 7c3bf31

Browse files
committed
更新样例
1 parent b15acf2 commit 7c3bf31

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

Chapter1/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.didispace</groupId>
77
<artifactId>Chapter1</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
8+
<version>1.0.0</version>
99
<packaging>jar</packaging>
1010

1111
<name>Chapter1</name>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.didispace.web;
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: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
package com.didispace;
22

3+
import com.didispace.web.HelloController;
4+
import org.junit.Before;
35
import org.junit.Test;
46
import org.junit.runner.RunWith;
57
import org.springframework.boot.test.SpringApplicationConfiguration;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.mock.web.MockServletContext;
610
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.equalTo;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19+
720

821
@RunWith(SpringJUnit4ClassRunner.class)
9-
@SpringApplicationConfiguration(classes = Chapter1Application.class)
22+
@SpringApplicationConfiguration(classes = MockServletContext.class)
23+
@WebAppConfiguration
1024
public class Chapter1ApplicationTests {
1125

26+
private MockMvc mvc;
27+
28+
@Before
29+
public void setUp() throws Exception {
30+
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
31+
}
32+
1233
@Test
13-
public void contextLoads() {
34+
public void getHello() throws Exception {
35+
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
36+
.andExpect(status().isOk())
37+
.andExpect(content().string(equalTo("Hello World")));
1438
}
1539

1640
}

0 commit comments

Comments
 (0)