Skip to content

Commit 4e74104

Browse files
committed
test: use WebMvcTest for InforControllerTest
1 parent 4e457dc commit 4e74104

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
package net.developerden.backend.controller;
22

33
import org.junit.jupiter.api.Test;
4-
54
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;
67
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;
108
import org.springframework.test.context.ActiveProfiles;
9+
import org.springframework.test.web.servlet.MockMvc;
1110

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;
1314

1415
@ActiveProfiles("test")
15-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
16+
@WebMvcTest
1617
public class InfoControllerTest {
1718

18-
@LocalServerPort
19-
private int port;
20-
2119
@Autowired
22-
private TestRestTemplate restTemplate;
23-
24-
private String baseUrl = "http://localhost:" + port;
20+
private MockMvc mockMvc;
2521

2622
@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+
3028
}
3129
}

0 commit comments

Comments
 (0)