Browse Source

Add missing debug(Writer) alternative

See gh-33059
pull/33073/head
Stéphane Nicoll 2 years ago
parent
commit
a1b0099795
  1. 11
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResultAssert.java
  2. 10
      spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java

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

@ -21,6 +21,7 @@ import java.io.OutputStream; @@ -21,6 +21,7 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import jakarta.servlet.http.Cookie;
import org.assertj.core.api.AbstractStringAssert;
@ -158,6 +159,16 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M @@ -158,6 +159,16 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M
return apply(MockMvcResultHandlers.print(stream));
}
/**
* Print {@link MvcResult} details to the supplied {@link Writer}.
* <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(Writer writer) {
return apply(MockMvcResultHandlers.print(writer));
}
/**
* Verify that the request has failed.
*/

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

@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.assertj; @@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.assertj;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
@ -357,6 +358,15 @@ public class MockMvcTesterIntegrationTests { @@ -357,6 +358,15 @@ public class MockMvcTesterIntegrationTests {
assertThat(capturedOut()).isEmpty();
}
@Test
void debugCanPrintToCustomWriter() {
StringWriter out = new StringWriter();
assertThat(mvc.get().uri("/greet")).debug(out).hasStatusOk();
assertThat(out.toString())
.contains("MockHttpServletRequest:", "MockHttpServletResponse:");
assertThat(capturedOut()).isEmpty();
}
private String capturedOut() {
return this.capturedOut.toString(StandardCharsets.UTF_8);
}

Loading…
Cancel
Save