Skip to content

Commit b71926b

Browse files
authored
Merge pull request JeffLi1993#73 from strongant/master
完善SpringBoot Hello World 单元测试
2 parents ea8b763 + cd6fa32 commit b71926b

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=-1
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
package demo.springboot;
22

3+
34
import org.junit.Test;
45
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
58
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.test.context.TestPropertySource;
611
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
14+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
717

818
@RunWith(SpringRunner.class)
9-
@SpringBootTest
19+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = QuickStartApplication.class)
20+
@AutoConfigureMockMvc
21+
@TestPropertySource(locations = "classpath:application.properties")
1022
public class QuickStartApplicationTests {
1123

12-
@Test
13-
public void contextLoads() {
14-
}
24+
@Autowired
25+
private MockMvc mvc;
26+
27+
28+
@Test
29+
public void requestHello_thenStatus200_and_outputHello() throws Exception {
30+
mvc.perform(get("/hello")
31+
.contentType(MediaType.APPLICATION_JSON))
32+
.andExpect(status().isOk())
33+
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN))
34+
.andExpect(content().encoding("UTF-8"))
35+
.andExpect(content().string("Hello,Spring Boot!"));
36+
}
1537

1638
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=-1

0 commit comments

Comments
 (0)