Only print MVC interactions when tests fail

Update `@AutoConfigureMockMvc` with a `printOnlyOnFailure` option which
allows errors to be printed only when tests fail. Defaults to `true`
meaning the logs are no longer cluttered with MVC results for passing
tests.

Fixes gh-6653
pull/6707/merge
Phillip Webb 8 years ago
parent 7ec14774a8
commit e239e64cb1

@ -61,6 +61,12 @@ public @interface AutoConfigureMockMvc {
@PropertyMapping(skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
MockMvcPrint print() default MockMvcPrint.DEFAULT;
/**
* If {@link MvcResult} information should be printed only if the test fails.
* @return {@code true} if printing only occurs on failure
*/
boolean printOnlyOnFailure() default true;
/**
* If a {@link WebClient} should be auto-configured when HtmlUnit is on the classpath.
* Defaults to {@code true}.

@ -0,0 +1,44 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.web.servlet;
import org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener;
/**
* {@link TestExecutionListener} used to print MVC lines only on failure.
*
* @author Phillip Webb
*/
class MockMvcPrintOnlyOnFailureTestExecutionListener
extends AbstractTestExecutionListener {
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
if (testContext.getTestException() != null) {
DeferredLinesWriter writer = DeferredLinesWriter
.get(testContext.getApplicationContext());
if (writer != null) {
writer.writeDeferredResult();
}
}
}
}

@ -17,16 +17,26 @@
package org.springframework.boot.test.autoconfigure.web.servlet;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.Filter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletContextInitializerBeans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultHandler;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.PrintingResultHandler;
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
import org.springframework.util.Assert;
@ -50,6 +60,8 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
private MockMvcPrint print = MockMvcPrint.DEFAULT;
private boolean printOnlyOnFailure = true;
/**
* Create a new {@link SpringBootMockMvcBuilderCustomizer} instance.
* @param context the source application context
@ -71,13 +83,25 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
}
private ResultHandler getPrintHandler() {
LinesWriter writer = getLinesWriter();
if (writer == null) {
return null;
}
if (this.printOnlyOnFailure) {
writer = new DeferredLinesWriter(this.context, writer);
}
return new LinesWritingResultHandler(writer);
}
private LinesWriter getLinesWriter() {
if (this.print == MockMvcPrint.NONE) {
return null;
}
if (this.print == MockMvcPrint.LOG_DEBUG) {
return MockMvcResultHandlers.log();
return new LoggingLinesWriter();
}
return new SystemResultHandler(this.print);
return new SystemLinesWriter(this.print);
}
private void addFilters(ConfigurableMockMvcBuilder<?> builder) {
@ -129,30 +153,51 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
return this.print;
}
public void setPrintOnlyOnFailure(boolean printOnlyOnFailure) {
this.printOnlyOnFailure = printOnlyOnFailure;
}
public boolean isPrintOnlyOnFailure() {
return this.printOnlyOnFailure;
}
/**
* {@link PrintingResultHandler} to deal with {@code System.out} and
* {@code System.err} printing. The actual {@link PrintStream} used to write the
* response is obtained as late as possible in case an {@code OutputCaptureRule} is
* being used.
* {@link ResultHandler} that prints {@link MvcResult} details to a given
* {@link LinesWriter}.
*/
private static class SystemResultHandler extends PrintingResultHandler {
private static class LinesWritingResultHandler implements ResultHandler {
private final LinesWriter writer;
LinesWritingResultHandler(LinesWriter writer) {
this.writer = writer;
}
protected SystemResultHandler(MockMvcPrint print) {
super(new SystemResultValuePrinter(print));
@Override
public void handle(MvcResult result) throws Exception {
LinesPrintingResultHandler delegate = new LinesPrintingResultHandler();
delegate.handle(result);
delegate.write(this.writer);
}
private static class SystemResultValuePrinter implements ResultValuePrinter {
private static class LinesPrintingResultHandler extends PrintingResultHandler {
private final MockMvcPrint print;
protected LinesPrintingResultHandler() {
super(new Printer());
}
SystemResultValuePrinter(MockMvcPrint print) {
this.print = print;
public void write(LinesWriter writer) {
writer.write(((Printer) getPrinter()).getLines());
}
private static class Printer implements ResultValuePrinter {
private final List<String> lines = new ArrayList<String>();
@Override
public void printHeading(String heading) {
getWriter().println();
getWriter().println(String.format("%s:", heading));
this.lines.add("");
this.lines.add(String.format("%s:", heading));
}
@Override
@ -160,10 +205,110 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
if (value != null && value.getClass().isArray()) {
value = CollectionUtils.arrayToList(value);
}
getWriter().println(String.format("%17s = %s", label, value));
this.lines.add(String.format("%17s = %s", label, value));
}
public List<String> getLines() {
return this.lines;
}
}
}
}
/**
* Strategy interface to write MVC result lines.
*/
interface LinesWriter {
void write(List<String> lines);
}
/**
* {@link LinesWriter} used to defer writing until errors are detected.
* @see MockMvcPrintOnlyOnFailureTestExecutionListener
*/
static class DeferredLinesWriter implements LinesWriter {
private static final String BEAN_NAME = DeferredLinesWriter.class.getName();
private final LinesWriter delegate;
private final List<String> lines = new ArrayList<String>();
DeferredLinesWriter(WebApplicationContext context, LinesWriter delegate) {
Assert.state(context instanceof ConfigurableApplicationContext,
"A ConfigurableApplicationContext is required for printOnlyOnFailure");
((ConfigurableApplicationContext) context).getBeanFactory()
.registerSingleton(BEAN_NAME, this);
this.delegate = delegate;
}
@Override
public void write(List<String> lines) {
this.lines.addAll(lines);
}
public void writeDeferredResult() {
this.delegate.write(this.lines);
}
public static DeferredLinesWriter get(ApplicationContext applicationContext) {
try {
return applicationContext.getBean(BEAN_NAME, DeferredLinesWriter.class);
}
catch (NoSuchBeanDefinitionException ex) {
return null;
}
}
}
/**
* {@link LinesWriter} to output results to the log.
*/
private static class LoggingLinesWriter implements LinesWriter {
private static final Log logger = LogFactory
.getLog("org.springframework.test.web.servlet.result");
@Override
public void write(List<String> lines) {
if (logger.isDebugEnabled()) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
for (String line : lines) {
printWriter.println(line);
}
logger.debug("MvcResult details:\n" + stringWriter);
}
}
}
/**
* {@link LinesWriter} to output results to {@code System.out} or {@code System.err}.
*/
private static class SystemLinesWriter implements LinesWriter {
private final MockMvcPrint print;
SystemLinesWriter(MockMvcPrint print) {
this.print = print;
}
@Override
public void write(List<String> lines) {
PrintStream printStream = getPrintStream();
for (String line : lines) {
printStream.println(line);
}
}
private PrintStream getWriter() {
private PrintStream getPrintStream() {
if (this.print == MockMvcPrint.SYSTEM_ERR) {
return System.err;
}
@ -173,5 +318,3 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
}
}
}

@ -82,4 +82,5 @@ org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomiz
org.springframework.test.context.TestExecutionListener=\
org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener,\
org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener

@ -42,7 +42,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(print = MockMvcPrint.SYSTEM_ERR)
@AutoConfigureMockMvc(print = MockMvcPrint.SYSTEM_ERR, printOnlyOnFailure = false)
@WithMockUser(username = "user", password = "secret")
public class MockMvcSpringBootTestIntegrationTests {

@ -0,0 +1,56 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.web.servlet;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Tests for {@link WebMvcTest} default print output.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest
@AutoConfigureMockMvc(secure = false, printOnlyOnFailure = false)
public class WebMvcTestPrintAlwaysIntegrationTests {
@Rule
public OutputCapture output = new OutputCapture();
@Autowired
private MockMvc mvc;
@Test
public void shouldPrint() throws Exception {
this.mvc.perform(get("/one")).andExpect(content().string("one"))
.andExpect(status().isOk());
assertThat(this.output.toString()).contains("Request URI = /one");
}
}

@ -16,16 +16,12 @@
package org.springframework.boot.test.autoconfigure.web.servlet;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -35,21 +31,24 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebMvcTest(secure = false)
@RunWith(WebMvcTestPrintDefaultRunner.class)
@WebMvcTest
@AutoConfigureMockMvc(secure = false)
public class WebMvcTestPrintDefaultIntegrationTests {
@Rule
public OutputCapture output = new OutputCapture();
@Autowired
private MockMvc mvc;
@Test
public void shouldPrint() throws Exception {
public void shouldNotPrint() throws Exception {
this.mvc.perform(get("/one")).andExpect(content().string("one"))
.andExpect(status().isOk());
assertThat(this.output.toString()).contains("Request URI = /one");
}
@Test
public void shouldPrint() throws Exception {
this.mvc.perform(get("/one")).andExpect(content().string("none"))
.andExpect(status().isOk());
}
}

@ -0,0 +1,77 @@
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.web.servlet;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
/**
* Test runner used for {@link WebMvcTestPrintDefaultIntegrationTests}.
*
* @author Phillip Webb
*/
public class WebMvcTestPrintDefaultRunner extends SpringJUnit4ClassRunner {
public WebMvcTestPrintDefaultRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
@Override
protected Statement methodBlock(FrameworkMethod frameworkMethod) {
Statement statement = super.methodBlock(frameworkMethod);
statement = new AlwaysPassStatement(statement);
OutputCapture outputCapture = new OutputCapture();
if (frameworkMethod.getName().equals("shouldPrint")) {
outputCapture.expect(containsString("HTTP Method"));
}
else if (frameworkMethod.getName().equals("shouldNotPrint")) {
outputCapture.expect(not(containsString("HTTP Method")));
}
else {
throw new IllegalStateException("Unexpected test method");
}
System.err.println(frameworkMethod.getName());
return outputCapture.apply(statement, null);
}
private static class AlwaysPassStatement extends Statement {
private final Statement delegate;
AlwaysPassStatement(Statement delegate) {
this.delegate = delegate;
}
@Override
public void evaluate() throws Throwable {
try {
this.delegate.evaluate();
}
catch (AssertionError ex) {
}
}
}
}
Loading…
Cancel
Save