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
58- public class MockMvcClientHttpRequestFactoryTests {
49+ @ SpringJUnitWebConfig
50+ class MockMvcClientHttpRequestFactoryTests {
5951
60- @ Autowired
61- private WebApplicationContext wac ;
52+ private final RestTemplate template ;
6253
63- private RestTemplate template ;
64-
65-
66- @ BeforeEach
67- public void setup () {
68- MockMvc mockMvc = MockMvcBuilders .webAppContextSetup (this .wac ).build ();
54+ MockMvcClientHttpRequestFactoryTests (WebApplicationContext wac ) {
55+ MockMvc mockMvc = MockMvcBuilders .webAppContextSetup (wac ).build ();
6956 this .template = new RestTemplate (new MockMvcClientHttpRequestFactory (mockMvc ));
7057 }
7158
59+
7260 @ Test
73- public void withResult () {
61+ void withResult () {
7462 assertThat (template .getForObject ("/foo" , String .class )).isEqualTo ("bar" );
7563 }
7664
7765 @ Test
78- public void withError () {
66+ void withError () {
7967 assertThatExceptionOfType (HttpClientErrorException .class )
8068 .isThrownBy (() -> template .getForEntity ("/error" , String .class ))
8169 .withMessageContaining ("400" )
8270 .withMessageContaining ("some bad request" );
8371 }
8472
8573 @ Test
86- public void withErrorAndBody () {
74+ void withErrorAndBody () {
8775 assertThatExceptionOfType (HttpClientErrorException .class )
8876 .isThrownBy (() -> template .getForEntity ("/errorbody" , String .class ))
8977 .withMessageContaining ("400" )
@@ -92,27 +80,27 @@ public void withErrorAndBody() {
9280
9381
9482 @ EnableWebMvc
95- @ Configuration
96- @ ComponentScan ( basePackageClasses = MockMvcClientHttpRequestFactoryTests .class )
83+ @ Configuration ( proxyBeanMethods = false )
84+ @ Import ( MyController .class )
9785 static class MyWebConfig implements WebMvcConfigurer {
9886 }
9987
10088 @ Controller
10189 static class MyController {
10290
103- @ RequestMapping ( value = "/foo" , method = RequestMethod . GET )
91+ @ GetMapping ( "/foo" )
10492 @ ResponseBody
105- public String handle () {
93+ String handle () {
10694 return "bar" ;
10795 }
10896
109- @ RequestMapping ( value = "/error" , method = RequestMethod . GET )
110- public void handleError (HttpServletResponse response ) throws Exception {
97+ @ GetMapping ( "/error" )
98+ void handleError (HttpServletResponse response ) throws Exception {
11199 response .sendError (400 , "some bad request" );
112100 }
113101
114- @ RequestMapping ( value = "/errorbody" , method = RequestMethod . GET )
115- public void handleErrorWithBody (HttpServletResponse response ) throws Exception {
102+ @ GetMapping ( "/errorbody" )
103+ void handleErrorWithBody (HttpServletResponse response ) throws Exception {
116104 response .sendError (400 , "some bad request" );
117105 response .getWriter ().write ("some really bad request" );
118106 }
0 commit comments