1717package org .springframework .test .web .client .samples ;
1818
1919import jakarta .servlet .http .HttpServletResponse ;
20- import org .junit .jupiter .api .BeforeEach ;
2120import org .junit .jupiter .api .Test ;
22- import org .junit .jupiter .api .extension .ExtendWith ;
2321
24- import org .springframework .beans .factory .annotation .Autowired ;
25- import org .springframework .context .annotation .ComponentScan ;
2622import org .springframework .context .annotation .Configuration ;
23+ import org .springframework .context .annotation .Import ;
2724import org .springframework .stereotype .Controller ;
28- import org .springframework .test .context .ContextConfiguration ;
29- import org .springframework .test .context .junit .jupiter .SpringExtension ;
30- import org .springframework .test .context .web .WebAppConfiguration ;
25+ import org .springframework .test .context .junit .jupiter .web .SpringJUnitWebConfig ;
3126import org .springframework .test .web .client .MockMvcClientHttpRequestFactory ;
3227import org .springframework .test .web .servlet .MockMvc ;
3328import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
34- import org .springframework .web .bind .annotation .RequestMapping ;
35- import org .springframework .web .bind .annotation .RequestMethod ;
29+ import org .springframework .web .bind .annotation .GetMapping ;
3630import org .springframework .web .bind .annotation .ResponseBody ;
3731import org .springframework .web .client .HttpClientErrorException ;
3832import org .springframework .web .client .RestTemplate ;
5246 * @author Rossen Stoyanchev
5347 * @author Juergen Hoeller
5448 */
55- @ ExtendWith (SpringExtension .class )
56- @ WebAppConfiguration
57- @ ContextConfiguration
49+ @ SpringJUnitWebConfig
5850@ SuppressWarnings ("deprecation" )
59- public class MockMvcClientHttpRequestFactoryTests {
51+ class MockMvcClientHttpRequestFactoryTests {
6052
61- @ Autowired
62- private WebApplicationContext wac ;
53+ private final RestTemplate template ;
6354
64- private RestTemplate template ;
65-
66-
67- @ BeforeEach
68- public void setup () {
69- MockMvc mockMvc = MockMvcBuilders .webAppContextSetup (this .wac ).build ();
55+ MockMvcClientHttpRequestFactoryTests (WebApplicationContext wac ) {
56+ MockMvc mockMvc = MockMvcBuilders .webAppContextSetup (wac ).build ();
7057 this .template = new RestTemplate (new MockMvcClientHttpRequestFactory (mockMvc ));
7158 }
7259
60+
7361 @ Test
74- public void withResult () {
62+ void withResult () {
7563 assertThat (template .getForObject ("/foo" , String .class )).isEqualTo ("bar" );
7664 }
7765
7866 @ Test
79- public void withError () {
67+ void withError () {
8068 assertThatExceptionOfType (HttpClientErrorException .class )
8169 .isThrownBy (() -> template .getForEntity ("/error" , String .class ))
8270 .withMessageContaining ("400" )
8371 .withMessageContaining ("some bad request" );
8472 }
8573
8674 @ Test
87- public void withErrorAndBody () {
75+ void withErrorAndBody () {
8876 assertThatExceptionOfType (HttpClientErrorException .class )
8977 .isThrownBy (() -> template .getForEntity ("/errorbody" , String .class ))
9078 .withMessageContaining ("400" )
@@ -93,27 +81,27 @@ public void withErrorAndBody() {
9381
9482
9583 @ EnableWebMvc
96- @ Configuration
97- @ ComponentScan ( basePackageClasses = MockMvcClientHttpRequestFactoryTests .class )
84+ @ Configuration ( proxyBeanMethods = false )
85+ @ Import ( MyController .class )
9886 static class MyWebConfig implements WebMvcConfigurer {
9987 }
10088
10189 @ Controller
10290 static class MyController {
10391
104- @ RequestMapping ( value = "/foo" , method = RequestMethod . GET )
92+ @ GetMapping ( "/foo" )
10593 @ ResponseBody
106- public String handle () {
94+ String handle () {
10795 return "bar" ;
10896 }
10997
110- @ RequestMapping ( value = "/error" , method = RequestMethod . GET )
111- public void handleError (HttpServletResponse response ) throws Exception {
98+ @ GetMapping ( "/error" )
99+ void handleError (HttpServletResponse response ) throws Exception {
112100 response .sendError (400 , "some bad request" );
113101 }
114102
115- @ RequestMapping ( value = "/errorbody" , method = RequestMethod . GET )
116- public void handleErrorWithBody (HttpServletResponse response ) throws Exception {
103+ @ GetMapping ( "/errorbody" )
104+ void handleErrorWithBody (HttpServletResponse response ) throws Exception {
117105 response .sendError (400 , "some bad request" );
118106 response .getWriter ().write ("some really bad request" );
119107 }
0 commit comments