Introduce LoggingResultHandler in Spring MVC Test

Prior to this commit, the Spring MVC Test framework only provided
support for printing debug information about the MvcResult to STDOUT.

This commit introduces support for logging `MvcResult` details at
`DEBUG` level via the Apache Commons Logging API. In addition, this
commit introduces additional `print(..)` variants for printing debug
information to custom output streams and writers.

Specifically, `MockMvcResultHandlers` has been augmented with the
following new static methods:

 - `log()`
 - `print(OutputStream)`
 - `print(Writer)`

Issue: SPR-13171
This commit is contained in:
Sam Brannen
2015-06-27 21:53:19 +02:00
parent 895d43a2b3
commit 693dcba867
6 changed files with 138 additions and 32 deletions

View File

@@ -42,10 +42,11 @@ import org.springframework.web.servlet.ModelAndView;
import static org.junit.Assert.*;
/**
* Tests for {@link PrintingResultHandler}.
* Unit tests for {@link PrintingResultHandler}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @see org.springframework.test.web.servlet.samples.standalone.resulthandlers.PrintingResultHandlerSmokeTests
*/
public class PrintingResultHandlerTests {

View File

@@ -16,6 +16,8 @@
package org.springframework.test.web.servlet.samples.standalone.resulthandlers;
import java.io.StringWriter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@@ -23,6 +25,7 @@ import org.junit.Ignore;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.result.PrintingResultHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -31,17 +34,38 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**
* Print debugging information about the executed request and response to System.out.
* Smoke test for {@link PrintingResultHandler}.
*
* <p>Prints debugging information about the executed request and response to
* various output streams.
*
* <p><strong>NOTE</strong>: this <em>smoke test</em> is not intended to be
* executed with the build. To run this test, comment out the {@code @Ignore}
* declaration and inspect the output manually.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @see org.springframework.test.web.servlet.result.PrintingResultHandlerTests
*/
@Ignore("Not intended to be executed with the build. Comment out this line to inspect the output manually.")
public class PrintingResultHandlerTests {
public class PrintingResultHandlerSmokeTests {
@Test
public void testPrint() throws Exception {
standaloneSetup(new SimpleController()).build().perform(get("/")).andDo(print());
StringWriter writer = new StringWriter();
standaloneSetup(new SimpleController())
.build()
.perform(get("/"))
.andDo(log())
.andDo(print())
.andDo(print(System.err))
.andDo(print(writer))
;
System.out.println();
System.out.println("===============================================================");
System.out.println(writer.toString());
}