File tree 2 files changed +21
-0
lines changed
main/java/org/springframework/test/web/servlet/assertj
test/java/org/springframework/test/web/servlet/assertj
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 21
21
import java .io .PrintWriter ;
22
22
import java .io .StringReader ;
23
23
import java .io .StringWriter ;
24
+ import java .io .Writer ;
24
25
25
26
import jakarta .servlet .http .Cookie ;
26
27
import org .assertj .core .api .AbstractStringAssert ;
@@ -158,6 +159,16 @@ public MvcTestResultAssert debug(OutputStream stream) {
158
159
return apply (MockMvcResultHandlers .print (stream ));
159
160
}
160
161
162
+ /**
163
+ * Print {@link MvcResult} details to the supplied {@link Writer}.
164
+ * <p>You must call it <b>before</b> calling the assertion otherwise it is ignored
165
+ * as the failing assertion breaks the chained call by throwing an
166
+ * AssertionError.
167
+ */
168
+ public MvcTestResultAssert debug (Writer writer ) {
169
+ return apply (MockMvcResultHandlers .print (writer ));
170
+ }
171
+
161
172
/**
162
173
* Verify that the request has failed.
163
174
*/
Original file line number Diff line number Diff line change 18
18
19
19
import java .io .ByteArrayOutputStream ;
20
20
import java .io .PrintStream ;
21
+ import java .io .StringWriter ;
21
22
import java .nio .charset .StandardCharsets ;
22
23
import java .util .Collections ;
23
24
import java .util .HashMap ;
@@ -357,6 +358,15 @@ void debugCanPrintToCustomOutputStream() {
357
358
assertThat (capturedOut ()).isEmpty ();
358
359
}
359
360
361
+ @ Test
362
+ void debugCanPrintToCustomWriter () {
363
+ StringWriter out = new StringWriter ();
364
+ assertThat (mvc .get ().uri ("/greet" )).debug (out ).hasStatusOk ();
365
+ assertThat (out .toString ())
366
+ .contains ("MockHttpServletRequest:" , "MockHttpServletResponse:" );
367
+ assertThat (capturedOut ()).isEmpty ();
368
+ }
369
+
360
370
private String capturedOut () {
361
371
return this .capturedOut .toString (StandardCharsets .UTF_8 );
362
372
}
You can’t perform that action at this time.
0 commit comments