Skip to content

Commit

Permalink
Add debug support for MvcResult
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Jun 18, 2024
1 parent 84bcb65 commit f2137c9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.test.web.servlet.assertj;

import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
Expand All @@ -37,6 +38,7 @@
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultHandler;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.web.servlet.ModelAndView;

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

/**
* Print {@link MvcResult} details to {@code System.out}.
* <p>You must call it <b>before</b> calling the assertion otherwise it is ignored
* as the failing assertion breaks the chained call by throwing an
* AssertionError.
*/
public MvcTestResultAssert debug() {
return debug(System.out);
}

/**
* Print {@link MvcResult} details to the supplied {@link OutputStream}.
* <p>You must call it <b>before</b> calling the assertion otherwise it is ignored
* as the failing assertion breaks the chained call by throwing an
* AssertionError.
*/
public MvcTestResultAssert debug(OutputStream stream) {
return apply(MockMvcResultHandlers.print(stream));
}

/**
* Verify that the request has failed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

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

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand All @@ -30,6 +33,8 @@
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -275,6 +280,44 @@ void isInvokedOn() {
}
}

@Nested
class DebugTests {

private final PrintStream standardOut = System.out;

private final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();

@BeforeEach
public void setUp() {
System.setOut(new PrintStream(capturedOut));
}

@AfterEach
public void tearDown() {
System.setOut(standardOut);
}

@Test
void debugUsesSystemOutByDefault() {
assertThat(mvc.get().uri("/greet")).debug().hasStatusOk();
assertThat(capturedOut()).contains("MockHttpServletRequest:", "MockHttpServletResponse:");
}

@Test
void debugCanPrintToCustomOutputStream() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
assertThat(mvc.get().uri("/greet")).debug(out).hasStatusOk();
assertThat(out.toString(StandardCharsets.UTF_8))
.contains("MockHttpServletRequest:", "MockHttpServletResponse:");
assertThat(capturedOut()).isEmpty();
}

private String capturedOut() {
return this.capturedOut.toString(StandardCharsets.UTF_8);
}

}

@Nested
class ExceptionTests {

Expand Down

0 comments on commit f2137c9

Please sign in to comment.