Skip to content

Commit e239e64

Browse files
committed
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
1 parent 7ec1477 commit e239e64

File tree

8 files changed

+365
-39
lines changed

8 files changed

+365
-39
lines changed

spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/AutoConfigureMockMvc.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
@PropertyMapping(skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
6262
MockMvcPrint print() default MockMvcPrint.DEFAULT;
6363

64+
/**
65+
* If {@link MvcResult} information should be printed only if the test fails.
66+
* @return {@code true} if printing only occurs on failure
67+
*/
68+
boolean printOnlyOnFailure() default true;
69+
6470
/**
6571
* If a {@link WebClient} should be auto-configured when HtmlUnit is on the classpath.
6672
* Defaults to {@code true}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.servlet;
18+
19+
import org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter;
20+
import org.springframework.test.context.TestContext;
21+
import org.springframework.test.context.TestExecutionListener;
22+
import org.springframework.test.context.support.AbstractTestExecutionListener;
23+
24+
/**
25+
* {@link TestExecutionListener} used to print MVC lines only on failure.
26+
*
27+
* @author Phillip Webb
28+
*/
29+
class MockMvcPrintOnlyOnFailureTestExecutionListener
30+
extends AbstractTestExecutionListener {
31+
32+
@Override
33+
public void afterTestMethod(TestContext testContext) throws Exception {
34+
if (testContext.getTestException() != null) {
35+
DeferredLinesWriter writer = DeferredLinesWriter
36+
.get(testContext.getApplicationContext());
37+
if (writer != null) {
38+
writer.writeDeferredResult();
39+
}
40+
}
41+
42+
}
43+
44+
}

spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java

Lines changed: 170 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,26 @@
1717
package org.springframework.boot.test.autoconfigure.web.servlet;
1818

1919
import java.io.PrintStream;
20+
import java.io.PrintWriter;
21+
import java.io.StringWriter;
22+
import java.util.ArrayList;
2023
import java.util.Collection;
24+
import java.util.List;
2125

2226
import javax.servlet.Filter;
2327

28+
import org.apache.commons.logging.Log;
29+
import org.apache.commons.logging.LogFactory;
30+
31+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2432
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
2533
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2634
import org.springframework.boot.web.servlet.ServletContextInitializer;
2735
import org.springframework.boot.web.servlet.ServletContextInitializerBeans;
36+
import org.springframework.context.ApplicationContext;
37+
import org.springframework.context.ConfigurableApplicationContext;
38+
import org.springframework.test.web.servlet.MvcResult;
2839
import org.springframework.test.web.servlet.ResultHandler;
29-
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
3040
import org.springframework.test.web.servlet.result.PrintingResultHandler;
3141
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
3242
import org.springframework.util.Assert;
@@ -50,6 +60,8 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
5060

5161
private MockMvcPrint print = MockMvcPrint.DEFAULT;
5262

63+
private boolean printOnlyOnFailure = true;
64+
5365
/**
5466
* Create a new {@link SpringBootMockMvcBuilderCustomizer} instance.
5567
* @param context the source application context
@@ -71,13 +83,25 @@ public void customize(ConfigurableMockMvcBuilder<?> builder) {
7183
}
7284

7385
private ResultHandler getPrintHandler() {
86+
LinesWriter writer = getLinesWriter();
87+
if (writer == null) {
88+
return null;
89+
}
90+
if (this.printOnlyOnFailure) {
91+
writer = new DeferredLinesWriter(this.context, writer);
92+
}
93+
return new LinesWritingResultHandler(writer);
94+
}
95+
96+
private LinesWriter getLinesWriter() {
7497
if (this.print == MockMvcPrint.NONE) {
7598
return null;
7699
}
77100
if (this.print == MockMvcPrint.LOG_DEBUG) {
78-
return MockMvcResultHandlers.log();
101+
return new LoggingLinesWriter();
79102
}
80-
return new SystemResultHandler(this.print);
103+
return new SystemLinesWriter(this.print);
104+
81105
}
82106

83107
private void addFilters(ConfigurableMockMvcBuilder<?> builder) {
@@ -129,47 +153,166 @@ public MockMvcPrint getPrint() {
129153
return this.print;
130154
}
131155

156+
public void setPrintOnlyOnFailure(boolean printOnlyOnFailure) {
157+
this.printOnlyOnFailure = printOnlyOnFailure;
158+
}
159+
160+
public boolean isPrintOnlyOnFailure() {
161+
return this.printOnlyOnFailure;
162+
}
163+
132164
/**
133-
* {@link PrintingResultHandler} to deal with {@code System.out} and
134-
* {@code System.err} printing. The actual {@link PrintStream} used to write the
135-
* response is obtained as late as possible in case an {@code OutputCaptureRule} is
136-
* being used.
165+
* {@link ResultHandler} that prints {@link MvcResult} details to a given
166+
* {@link LinesWriter}.
137167
*/
138-
private static class SystemResultHandler extends PrintingResultHandler {
168+
private static class LinesWritingResultHandler implements ResultHandler {
169+
170+
private final LinesWriter writer;
139171

140-
protected SystemResultHandler(MockMvcPrint print) {
141-
super(new SystemResultValuePrinter(print));
172+
LinesWritingResultHandler(LinesWriter writer) {
173+
this.writer = writer;
142174
}
143175

144-
private static class SystemResultValuePrinter implements ResultValuePrinter {
176+
@Override
177+
public void handle(MvcResult result) throws Exception {
178+
LinesPrintingResultHandler delegate = new LinesPrintingResultHandler();
179+
delegate.handle(result);
180+
delegate.write(this.writer);
181+
}
145182

146-
private final MockMvcPrint print;
183+
private static class LinesPrintingResultHandler extends PrintingResultHandler {
147184

148-
SystemResultValuePrinter(MockMvcPrint print) {
149-
this.print = print;
185+
protected LinesPrintingResultHandler() {
186+
super(new Printer());
150187
}
151188

152-
@Override
153-
public void printHeading(String heading) {
154-
getWriter().println();
155-
getWriter().println(String.format("%s:", heading));
189+
public void write(LinesWriter writer) {
190+
writer.write(((Printer) getPrinter()).getLines());
156191
}
157192

158-
@Override
159-
public void printValue(String label, Object value) {
160-
if (value != null && value.getClass().isArray()) {
161-
value = CollectionUtils.arrayToList(value);
193+
private static class Printer implements ResultValuePrinter {
194+
195+
private final List<String> lines = new ArrayList<String>();
196+
197+
@Override
198+
public void printHeading(String heading) {
199+
this.lines.add("");
200+
this.lines.add(String.format("%s:", heading));
162201
}
163-
getWriter().println(String.format("%17s = %s", label, value));
202+
203+
@Override
204+
public void printValue(String label, Object value) {
205+
if (value != null && value.getClass().isArray()) {
206+
value = CollectionUtils.arrayToList(value);
207+
}
208+
this.lines.add(String.format("%17s = %s", label, value));
209+
}
210+
211+
public List<String> getLines() {
212+
return this.lines;
213+
}
214+
215+
}
216+
217+
}
218+
219+
}
220+
221+
/**
222+
* Strategy interface to write MVC result lines.
223+
*/
224+
interface LinesWriter {
225+
226+
void write(List<String> lines);
227+
228+
}
229+
230+
/**
231+
* {@link LinesWriter} used to defer writing until errors are detected.
232+
* @see MockMvcPrintOnlyOnFailureTestExecutionListener
233+
*/
234+
static class DeferredLinesWriter implements LinesWriter {
235+
236+
private static final String BEAN_NAME = DeferredLinesWriter.class.getName();
237+
238+
private final LinesWriter delegate;
239+
240+
private final List<String> lines = new ArrayList<String>();
241+
242+
DeferredLinesWriter(WebApplicationContext context, LinesWriter delegate) {
243+
Assert.state(context instanceof ConfigurableApplicationContext,
244+
"A ConfigurableApplicationContext is required for printOnlyOnFailure");
245+
((ConfigurableApplicationContext) context).getBeanFactory()
246+
.registerSingleton(BEAN_NAME, this);
247+
this.delegate = delegate;
248+
}
249+
250+
@Override
251+
public void write(List<String> lines) {
252+
this.lines.addAll(lines);
253+
}
254+
255+
public void writeDeferredResult() {
256+
this.delegate.write(this.lines);
257+
}
258+
259+
public static DeferredLinesWriter get(ApplicationContext applicationContext) {
260+
try {
261+
return applicationContext.getBean(BEAN_NAME, DeferredLinesWriter.class);
262+
}
263+
catch (NoSuchBeanDefinitionException ex) {
264+
return null;
164265
}
266+
}
267+
268+
}
165269

166-
private PrintStream getWriter() {
167-
if (this.print == MockMvcPrint.SYSTEM_ERR) {
168-
return System.err;
270+
/**
271+
* {@link LinesWriter} to output results to the log.
272+
*/
273+
private static class LoggingLinesWriter implements LinesWriter {
274+
275+
private static final Log logger = LogFactory
276+
.getLog("org.springframework.test.web.servlet.result");
277+
278+
@Override
279+
public void write(List<String> lines) {
280+
if (logger.isDebugEnabled()) {
281+
StringWriter stringWriter = new StringWriter();
282+
PrintWriter printWriter = new PrintWriter(stringWriter);
283+
for (String line : lines) {
284+
printWriter.println(line);
169285
}
170-
return System.out;
286+
logger.debug("MvcResult details:\n" + stringWriter);
287+
}
288+
}
289+
290+
}
291+
292+
/**
293+
* {@link LinesWriter} to output results to {@code System.out} or {@code System.err}.
294+
*/
295+
private static class SystemLinesWriter implements LinesWriter {
296+
297+
private final MockMvcPrint print;
298+
299+
SystemLinesWriter(MockMvcPrint print) {
300+
this.print = print;
301+
}
302+
303+
@Override
304+
public void write(List<String> lines) {
305+
PrintStream printStream = getPrintStream();
306+
for (String line : lines) {
307+
printStream.println(line);
171308
}
309+
}
172310

311+
private PrintStream getPrintStream() {
312+
if (this.print == MockMvcPrint.SYSTEM_ERR) {
313+
return System.err;
314+
}
315+
return System.out;
173316
}
174317

175318
}

spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,5 @@ org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomiz
8282
org.springframework.test.context.TestExecutionListener=\
8383
org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener,\
8484
org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\
85+
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\
8586
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener

spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcSpringBootTestIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*/
4343
@RunWith(SpringRunner.class)
4444
@SpringBootTest
45-
@AutoConfigureMockMvc(print = MockMvcPrint.SYSTEM_ERR)
45+
@AutoConfigureMockMvc(print = MockMvcPrint.SYSTEM_ERR, printOnlyOnFailure = false)
4646
@WithMockUser(username = "user", password = "secret")
4747
public class MockMvcSpringBootTestIntegrationTests {
4848

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.servlet;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.test.rule.OutputCapture;
25+
import org.springframework.test.context.junit4.SpringRunner;
26+
import org.springframework.test.web.servlet.MockMvc;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
32+
33+
/**
34+
* Tests for {@link WebMvcTest} default print output.
35+
*
36+
* @author Phillip Webb
37+
*/
38+
@RunWith(SpringRunner.class)
39+
@WebMvcTest
40+
@AutoConfigureMockMvc(secure = false, printOnlyOnFailure = false)
41+
public class WebMvcTestPrintAlwaysIntegrationTests {
42+
43+
@Rule
44+
public OutputCapture output = new OutputCapture();
45+
46+
@Autowired
47+
private MockMvc mvc;
48+
49+
@Test
50+
public void shouldPrint() throws Exception {
51+
this.mvc.perform(get("/one")).andExpect(content().string("one"))
52+
.andExpect(status().isOk());
53+
assertThat(this.output.toString()).contains("Request URI = /one");
54+
}
55+
56+
}

0 commit comments

Comments
 (0)