Skip to content

Commit f2137c9

Browse files
committed
Add debug support for MvcResult
This commit adds a debug() shortcut to print the detail of a MvcResult to ease debugging when a request did not work as expected. Closes gh-33059
1 parent 84bcb65 commit f2137c9

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.test.web.servlet.assertj;
1818

1919
import java.io.BufferedReader;
20+
import java.io.OutputStream;
2021
import java.io.PrintWriter;
2122
import java.io.StringReader;
2223
import java.io.StringWriter;
@@ -37,6 +38,7 @@
3738
import org.springframework.test.web.servlet.MvcResult;
3839
import org.springframework.test.web.servlet.ResultHandler;
3940
import org.springframework.test.web.servlet.ResultMatcher;
41+
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
4042
import org.springframework.web.servlet.ModelAndView;
4143

4244
/**
@@ -136,6 +138,26 @@ public ObjectAssert<Object> asyncResult() {
136138
return Assertions.assertThat(getMvcResult().getAsyncResult()).as("Async result");
137139
}
138140

141+
/**
142+
* Print {@link MvcResult} details to {@code System.out}.
143+
* <p>You must call it <b>before</b> calling the assertion otherwise it is ignored
144+
* as the failing assertion breaks the chained call by throwing an
145+
* AssertionError.
146+
*/
147+
public MvcTestResultAssert debug() {
148+
return debug(System.out);
149+
}
150+
151+
/**
152+
* Print {@link MvcResult} details to the supplied {@link OutputStream}.
153+
* <p>You must call it <b>before</b> calling the assertion otherwise it is ignored
154+
* as the failing assertion breaks the chained call by throwing an
155+
* AssertionError.
156+
*/
157+
public MvcTestResultAssert debug(OutputStream stream) {
158+
return apply(MockMvcResultHandlers.print(stream));
159+
}
160+
139161
/**
140162
* Verify that the request has failed.
141163
*/

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

+43
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.test.web.servlet.assertj;
1818

19+
import java.io.ByteArrayOutputStream;
20+
import java.io.PrintStream;
21+
import java.nio.charset.StandardCharsets;
1922
import java.util.Collections;
2023
import java.util.List;
2124
import java.util.Locale;
@@ -30,6 +33,8 @@
3033
import jakarta.servlet.http.HttpServletResponse;
3134
import jakarta.validation.Valid;
3235
import jakarta.validation.constraints.Size;
36+
import org.junit.jupiter.api.AfterEach;
37+
import org.junit.jupiter.api.BeforeEach;
3338
import org.junit.jupiter.api.Nested;
3439
import org.junit.jupiter.api.Test;
3540

@@ -275,6 +280,44 @@ void isInvokedOn() {
275280
}
276281
}
277282

283+
@Nested
284+
class DebugTests {
285+
286+
private final PrintStream standardOut = System.out;
287+
288+
private final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();
289+
290+
@BeforeEach
291+
public void setUp() {
292+
System.setOut(new PrintStream(capturedOut));
293+
}
294+
295+
@AfterEach
296+
public void tearDown() {
297+
System.setOut(standardOut);
298+
}
299+
300+
@Test
301+
void debugUsesSystemOutByDefault() {
302+
assertThat(mvc.get().uri("/greet")).debug().hasStatusOk();
303+
assertThat(capturedOut()).contains("MockHttpServletRequest:", "MockHttpServletResponse:");
304+
}
305+
306+
@Test
307+
void debugCanPrintToCustomOutputStream() {
308+
ByteArrayOutputStream out = new ByteArrayOutputStream();
309+
assertThat(mvc.get().uri("/greet")).debug(out).hasStatusOk();
310+
assertThat(out.toString(StandardCharsets.UTF_8))
311+
.contains("MockHttpServletRequest:", "MockHttpServletResponse:");
312+
assertThat(capturedOut()).isEmpty();
313+
}
314+
315+
private String capturedOut() {
316+
return this.capturedOut.toString(StandardCharsets.UTF_8);
317+
}
318+
319+
}
320+
278321
@Nested
279322
class ExceptionTests {
280323

0 commit comments

Comments
 (0)