|
1 | 1 | package net.developerden.backend.controller;
|
2 | 2 |
|
3 | 3 | import org.junit.jupiter.api.Test;
|
4 |
| - |
5 | 4 | import org.springframework.beans.factory.annotation.Autowired;
|
| 5 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 6 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
6 | 7 | import org.springframework.boot.test.context.SpringBootTest;
|
7 |
| -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
8 |
| -import org.springframework.boot.test.web.client.TestRestTemplate; |
9 |
| -import org.springframework.boot.web.server.LocalServerPort; |
10 | 8 | import org.springframework.test.context.ActiveProfiles;
|
| 9 | +import org.springframework.test.web.servlet.MockMvc; |
11 | 10 |
|
12 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 12 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 13 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
13 | 14 |
|
14 | 15 | @ActiveProfiles("test")
|
15 |
| -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
| 16 | +@WebMvcTest |
16 | 17 | public class InfoControllerTest {
|
17 | 18 |
|
18 |
| - @LocalServerPort |
19 |
| - private int port; |
20 |
| - |
21 | 19 | @Autowired
|
22 |
| - private TestRestTemplate restTemplate; |
23 |
| - |
24 |
| - private String baseUrl = "http://localhost:" + port; |
| 20 | + private MockMvc mockMvc; |
25 | 21 |
|
26 | 22 | @Test
|
27 |
| - public void ping_happyDay_returnsPong() { |
28 |
| - String response = this.restTemplate.getForObject(baseUrl + "/ping", String.class); |
29 |
| - assertThat(response).isEqualTo("pong"); |
| 23 | + public void ping_happyDay_returnsPong() throws Exception { |
| 24 | + this.mockMvc.perform(get("/ping")) |
| 25 | + .andExpect(status().isOk()) |
| 26 | + .andExpect(content().string("pong")); |
| 27 | + |
30 | 28 | }
|
31 | 29 | }
|
0 commit comments