File tree Expand file tree Collapse file tree 5 files changed +127
-0
lines changed
01-testing-spring-boot/src
main/java/com/stacktips/movies
test/java/com/stacktips/movies/api Expand file tree Collapse file tree 5 files changed +127
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .stacktips .movies .api ;
2+
3+ import com .stacktips .movies .service .GreetingService ;
4+ import org .springframework .web .bind .annotation .RequestMapping ;
5+ import org .springframework .web .bind .annotation .RestController ;
6+
7+ @ RestController
8+ public class GreetingController {
9+
10+ private final GreetingService greetingService ;
11+
12+ public GreetingController (GreetingService greetingService ) {
13+ this .greetingService = greetingService ;
14+ }
15+
16+ @ RequestMapping ("/" )
17+ public String greeting () {
18+ return greetingService .greet ();
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .stacktips .movies .service ;
2+
3+ import org .springframework .stereotype .Service ;
4+
5+ @ Service
6+ public class GreetingService {
7+ public String greet () {
8+ return "Hello, World!" ;
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ package com .stacktips .movies .api ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
6+ import org .springframework .boot .test .context .SpringBootTest ;
7+ import org .springframework .test .web .servlet .MockMvc ;
8+
9+ import static org .hamcrest .Matchers .containsString ;
10+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
11+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
12+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
13+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
14+
15+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
16+ @ AutoConfigureMockMvc
17+ class GreetingControllerUsingMockMvcTest {
18+
19+ @ Autowired
20+ MockMvc mockMvc ;
21+
22+ @ Test
23+ void greetingShouldReturnDefaultMessage () throws Exception {
24+ this .mockMvc .perform (get ("/" ))
25+ .andDo (print ())
26+ .andExpect (status ().isOk ())
27+ .andExpect (content ().string (containsString ("Hello, World" )));
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ package com .stacktips .movies .api ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .boot .test .context .SpringBootTest ;
6+ import org .springframework .boot .test .web .client .TestRestTemplate ;
7+ import org .springframework .boot .test .web .server .LocalServerPort ;
8+
9+ import static org .hamcrest .MatcherAssert .assertThat ;
10+ import static org .hamcrest .Matchers .is ;
11+ import static org .springframework .boot .test .context .SpringBootTest .WebEnvironment ;
12+
13+ @ SpringBootTest (webEnvironment = WebEnvironment .RANDOM_PORT )
14+ class GreetingsControllerTest {
15+
16+ @ LocalServerPort
17+ private int port ;
18+
19+ @ Autowired
20+ private TestRestTemplate restTemplate ;
21+
22+ @ Test
23+ void greetingShouldReturnDefaultMessage () throws Exception {
24+ String result = this .restTemplate .getForObject ("http://localhost:" + port + "/" ,
25+ String .class );
26+ assertThat (result , is ("Hello, World" ));
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ package com .stacktips .movies .api ;
2+
3+ import com .stacktips .movies .service .MovieService ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
7+ import org .springframework .boot .test .context .SpringBootTest ;
8+ import org .springframework .boot .test .mock .mockito .MockBean ;
9+ import org .springframework .test .web .servlet .MockMvc ;
10+
11+ import static org .hamcrest .Matchers .containsString ;
12+ import static org .mockito .Mockito .when ;
13+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
14+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
15+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
16+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
17+
18+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
19+ @ AutoConfigureMockMvc
20+ class GreetingControllerTest {
21+
22+ @ Autowired
23+ MockMvc mockMvc ;
24+
25+ @ MockBean
26+ private MovieService movieService ;
27+
28+ @ Test
29+ void greetingShouldReturnMockResponse () throws Exception {
30+ when (movieService .greet ()).thenReturn ("Hello, Spring Boot!" );
31+
32+ this .mockMvc .perform (get ("/" ))
33+ .andDo (print ())
34+ .andExpect (status ().isOk ())
35+ .andExpect (content ().string (containsString ("Hello, Spring Boot!" )));
36+ }
37+
38+ }
You can’t perform that action at this time.
0 commit comments