16
16
package org .springframework .data .rest .tests ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
- import static org .hamcrest .CoreMatchers .*;
20
19
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
21
- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
22
20
23
21
import io .micrometer .observation .ObservationRegistry ;
24
22
import jakarta .servlet .Filter ;
37
35
38
36
import org .junit .jupiter .api .BeforeEach ;
39
37
import org .junit .jupiter .api .extension .ExtendWith ;
38
+
40
39
import org .springframework .beans .factory .annotation .Autowired ;
41
40
import org .springframework .data .rest .webmvc .config .RepositoryRestMvcConfiguration ;
42
41
import org .springframework .hateoas .Link ;
43
42
import org .springframework .hateoas .LinkRelation ;
44
43
import org .springframework .hateoas .client .LinkDiscoverers ;
44
+ import org .springframework .http .HttpHeaders ;
45
45
import org .springframework .http .HttpMethod ;
46
+ import org .springframework .http .HttpStatus ;
46
47
import org .springframework .http .MediaType ;
47
48
import org .springframework .http .server .observation .ServerRequestObservationContext ;
48
49
import org .springframework .mock .web .MockHttpServletResponse ;
51
52
import org .springframework .test .context .web .WebAppConfiguration ;
52
53
import org .springframework .test .web .servlet .MockMvc ;
53
54
import org .springframework .test .web .servlet .ResultMatcher ;
55
+ import org .springframework .test .web .servlet .assertj .MockMvcTester ;
56
+ import org .springframework .test .web .servlet .assertj .MvcTestResult ;
54
57
import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
55
58
import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
56
59
import org .springframework .util .LinkedMultiValueMap ;
@@ -84,48 +87,48 @@ public abstract class AbstractWebIntegrationTests {
84
87
@ Autowired LinkDiscoverers discoverers ;
85
88
86
89
protected TestMvcClient client ;
87
- protected MockMvc mvc ;
90
+ protected MockMvc mockMvc ;
91
+ protected MockMvcTester mvc ;
88
92
protected ServerRequestObservationContext observationContext ;
89
93
90
94
@ BeforeEach
91
95
public void setUp () {
92
96
setupMockMvc ();
93
- this .client = new TestMvcClient (mvc , discoverers );
97
+ this .client = new TestMvcClient (mockMvc , discoverers );
94
98
}
95
99
96
100
protected void setupMockMvc () {
97
- this .mvc = MockMvcBuilders .webAppContextSetup (context ) //
101
+ this .mockMvc = MockMvcBuilders .webAppContextSetup (context ) //
98
102
.defaultRequest (get ("/" ).accept (TestMvcClient .DEFAULT_MEDIA_TYPE )) //
99
103
.addFilters (new FilterImplementation ()) //
100
104
.build ();
105
+ this .mvc = MockMvcTester .create (mockMvc );
101
106
}
102
107
103
108
protected MockHttpServletResponse postAndGet (Link link , Object payload , MediaType mediaType ) throws Exception {
104
109
105
110
String href = link .isTemplated () ? link .expand ().getHref () : link .getHref ();
106
111
107
- MockHttpServletResponse response = mvc .perform (post (href ).content (payload .toString ()).contentType (mediaType ))//
108
- .andExpect (status ().isCreated ())//
109
- .andExpect (header ().string ("Location" , is (notNullValue ())))//
110
- .andReturn ().getResponse ();
112
+ MvcTestResult result = mvc .perform (post (href ).content (payload .toString ()).contentType (mediaType ));
113
+ assertThat (result ).hasStatus (HttpStatus .CREATED ).headers ().containsHeader (HttpHeaders .LOCATION );
111
114
112
- String content = response .getContentAsString ();
115
+ String content = result . getResponse () .getContentAsString ();
113
116
114
117
if (StringUtils .hasText (content )) {
115
- return response ;
118
+ return result . getResponse () ;
116
119
}
117
120
118
- return client .request (response .getHeader ("Location" ));
121
+ return client .request (result . getResponse () .getHeader ("Location" ));
119
122
}
120
123
121
124
protected MockHttpServletResponse putAndGet (Link link , Object payload , MediaType mediaType ) throws Exception {
122
125
123
126
String href = link .isTemplated () ? link .expand ().getHref () : link .getHref ();
124
127
125
- MockHttpServletResponse response = mvc .perform (put (href ).content (payload .toString ()).contentType (mediaType ))//
126
- .andExpect (status ().is2xxSuccessful ())//
127
- .andReturn ().getResponse ();
128
+ MvcTestResult result = mvc .perform (put (href ).content (payload .toString ()).contentType (mediaType ));
129
+ assertThat (result ).hasStatus2xxSuccessful ();
128
130
131
+ MockHttpServletResponse response = result .getResponse ();
129
132
return StringUtils .hasText (response .getContentAsString ()) ? response : client .request (link );
130
133
}
131
134
@@ -134,9 +137,10 @@ protected MockHttpServletResponse putOnlyExpect5XXStatus(Link link, Object paylo
134
137
135
138
String href = link .isTemplated () ? link .expand ().getHref () : link .getHref ();
136
139
137
- MockHttpServletResponse response = mvc .perform (put (href ).content (payload .toString ()).contentType (mediaType ))//
138
- .andExpect (status ().is5xxServerError ())//
139
- .andReturn ().getResponse ();
140
+ MvcTestResult result = mvc .perform (put (href ).content (payload .toString ()).contentType (mediaType ));
141
+ assertThat (result ).hasStatus5xxServerError ();
142
+
143
+ MockHttpServletResponse response = result .getResponse ();
140
144
141
145
return StringUtils .hasText (response .getContentAsString ()) ? response : client .request (link );
142
146
}
@@ -145,9 +149,11 @@ protected MockHttpServletResponse patchAndGet(Link link, Object payload, MediaTy
145
149
146
150
String href = link .isTemplated () ? link .expand ().getHref () : link .getHref ();
147
151
148
- MockHttpServletResponse response = mvc .perform (MockMvcRequestBuilders .request (HttpMethod .PATCH , href ).//
149
- content (payload .toString ()).contentType (mediaType )).andExpect (status ().is2xxSuccessful ())//
150
- .andReturn ().getResponse ();
152
+ MvcTestResult result = mvc .perform (MockMvcRequestBuilders .request (HttpMethod .PATCH , href ).//
153
+ content (payload .toString ()).contentType (mediaType ));
154
+ assertThat (result ).hasStatus2xxSuccessful ();
155
+
156
+ MockHttpServletResponse response = result .getResponse ();
151
157
152
158
return StringUtils .hasText (response .getContentAsString ()) ? response : client .request (href );
153
159
}
@@ -156,13 +162,13 @@ protected void deleteAndVerify(Link link) throws Exception {
156
162
157
163
String href = link .isTemplated () ? link .expand ().getHref () : link .getHref ();
158
164
159
- mvc .perform (delete (href ))//
160
- .andExpect (status ().isNoContent ())//
161
- .andReturn ().getResponse ();
165
+ MvcTestResult result = mvc .perform (delete (href ));
166
+ assertThat (result ).hasStatus (HttpStatus .NO_CONTENT );
167
+
168
+ MockHttpServletResponse response = result .getResponse ();
162
169
163
170
// Check that the resource is unavailable after a DELETE
164
- mvc .perform (get (href ))//
165
- .andExpect (status ().isNotFound ());
171
+ assertThat (mvc .perform (get (href ))).hasStatus (HttpStatus .NOT_FOUND );//
166
172
}
167
173
168
174
protected Link assertHasContentLinkWithRel (LinkRelation relation , MockHttpServletResponse response ) throws Exception {
0 commit comments