Skip to content

Commit a1b0099

Browse files
committed
Add missing debug(Writer) alternative
See gh-33059
1 parent 54c37ff commit a1b0099

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResultAssert.java

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.PrintWriter;
2222
import java.io.StringReader;
2323
import java.io.StringWriter;
24+
import java.io.Writer;
2425

2526
import jakarta.servlet.http.Cookie;
2627
import org.assertj.core.api.AbstractStringAssert;
@@ -158,6 +159,16 @@ public MvcTestResultAssert debug(OutputStream stream) {
158159
return apply(MockMvcResultHandlers.print(stream));
159160
}
160161

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+
161172
/**
162173
* Verify that the request has failed.
163174
*/

spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.PrintStream;
21+
import java.io.StringWriter;
2122
import java.nio.charset.StandardCharsets;
2223
import java.util.Collections;
2324
import java.util.HashMap;
@@ -357,6 +358,15 @@ void debugCanPrintToCustomOutputStream() {
357358
assertThat(capturedOut()).isEmpty();
358359
}
359360

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+
360370
private String capturedOut() {
361371
return this.capturedOut.toString(StandardCharsets.UTF_8);
362372
}

0 commit comments

Comments
 (0)