|
1 | 1 | package demo.springboot;
|
2 | 2 |
|
| 3 | + |
3 | 4 | import org.junit.Test;
|
4 | 5 | import org.junit.runner.RunWith;
|
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
5 | 8 | import org.springframework.boot.test.context.SpringBootTest;
|
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.test.context.TestPropertySource; |
6 | 11 | 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; |
7 | 17 |
|
8 | 18 | @RunWith(SpringRunner.class)
|
9 |
| -@SpringBootTest |
| 19 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = QuickStartApplication.class) |
| 20 | +@AutoConfigureMockMvc |
| 21 | +@TestPropertySource(locations = "classpath:application.properties") |
10 | 22 | public class QuickStartApplicationTests {
|
11 | 23 |
|
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 | + } |
15 | 37 |
|
16 | 38 | }
|
0 commit comments