|
1 | 1 | package com.bnc.sbjb.rest;
|
2 | 2 |
|
3 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 3 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 4 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 5 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
4 | 6 |
|
| 7 | +import org.junit.jupiter.api.DisplayName; |
5 | 8 | import org.junit.jupiter.api.Test;
|
6 | 9 | import org.junit.jupiter.api.extension.ExtendWith;
|
7 | 10 | import org.springframework.beans.factory.annotation.Autowired;
|
8 | 11 | import org.springframework.beans.factory.annotation.Value;
|
9 |
| -import org.springframework.boot.test.context.SpringBootTest; |
10 |
| -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
11 |
| -import org.springframework.boot.test.web.client.TestRestTemplate; |
| 12 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
12 | 13 | import org.springframework.test.context.junit.jupiter.SpringExtension;
|
| 14 | +import org.springframework.test.web.servlet.MockMvc; |
| 15 | +import org.springframework.test.web.servlet.ResultMatcher; |
13 | 16 |
|
14 |
| -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
15 | 17 | @ExtendWith(SpringExtension.class)
|
16 |
| -public class ControllerTest { |
| 18 | +@WebMvcTest(Controller.class) |
| 19 | +class ControllerTest { |
17 | 20 |
|
18 | 21 | @Value("${resource.path}")
|
19 | 22 | private String basePath;
|
20 | 23 |
|
21 | 24 | @Autowired
|
22 |
| - private TestRestTemplate template; |
| 25 | + private MockMvc mockMvc; |
| 26 | + |
| 27 | + private ResultMatcher equals(String string) { |
| 28 | + return content().string(string); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + @DisplayName("Spring says hello world") |
| 33 | + void testHelloWorld() throws Exception { |
| 34 | + mockMvc.perform(get(basePath + "/hello")).andExpect(equals("Hello World")); |
| 35 | + } |
23 | 36 |
|
24 | 37 | @Test
|
25 |
| - void testHelloWorld() { |
26 |
| - String actual = template.getForObject(basePath + "/hello", String.class); |
27 |
| - assertThat(actual).isEqualTo("Hello World"); |
| 38 | + @DisplayName("Spring handles errors gracefully") |
| 39 | + void testNotYetWorld() throws Exception { |
| 40 | + mockMvc.perform(get(basePath + "/hello-error")).andExpect(status().is5xxServerError()); |
28 | 41 | }
|
29 | 42 | }
|
0 commit comments