From 4a1e0d4544d9aef51c3a668930f0e2f7e87eae26 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 25 Jul 2016 11:45:19 -0700 Subject: [PATCH] Extend @AutoConfigureMockMvc print support Update @AutoConfigureMockMvc to support extended print options including `System.err` and `log.debug`. In addition the "default" print option can now be overridden by adding `spring.test.mockmvc.print=...` to `src/test/resources/application.properties`. Fixes gh-6455 --- .../hypermedia/EndpointDocumentation.java | 3 +- .../src/test/resources/application.properties | 1 + .../web/servlet/AutoConfigureMockMvc.java | 7 +- .../web/servlet/MockMvcPrint.java | 55 ++++++++++++++ .../SpringBootMockMvcBuilderCustomizer.java | 74 +++++++++++++++++-- ...MockMvcSpringBootTestIntegrationTests.java | 8 +- ...ebMvcTestPrintDefaultIntegrationTests.java | 55 ++++++++++++++ ...tPrintDefaultOverrideIntegrationTests.java | 57 ++++++++++++++ ...bMvcTestPrintOverrideIntegrationTests.java | 56 ++++++++++++++ 9 files changed, 303 insertions(+), 13 deletions(-) create mode 100644 spring-boot-samples/spring-boot-sample-test/src/test/resources/application.properties create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcPrint.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintDefaultIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintDefaultOverrideIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintOverrideIntegrationTests.java diff --git a/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/EndpointDocumentation.java b/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/EndpointDocumentation.java index 338b2341659..79a931a302e 100644 --- a/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/EndpointDocumentation.java +++ b/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/EndpointDocumentation.java @@ -41,6 +41,7 @@ import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints; import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint; import org.springframework.boot.test.context.SpringBootContextLoader; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -66,7 +67,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. "endpoints.health.sensitive=true", "endpoints.actuator.enabled=false" }) @DirtiesContext @AutoConfigureRestDocs(EndpointDocumentation.RESTDOCS_OUTPUT_DIR) -@AutoConfigureMockMvc(alwaysPrint = false) +@AutoConfigureMockMvc(print = MockMvcPrint.NONE) public class EndpointDocumentation { static final String RESTDOCS_OUTPUT_DIR = "target/generated-snippets"; diff --git a/spring-boot-samples/spring-boot-sample-test/src/test/resources/application.properties b/spring-boot-samples/spring-boot-sample-test/src/test/resources/application.properties new file mode 100644 index 00000000000..24dfc94a0ce --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-test/src/test/resources/application.properties @@ -0,0 +1 @@ +spring.test.mockmvc.print=none diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/AutoConfigureMockMvc.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/AutoConfigureMockMvc.java index 69a4211df5c..8d316a316c9 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/AutoConfigureMockMvc.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/AutoConfigureMockMvc.java @@ -54,11 +54,10 @@ public @interface AutoConfigureMockMvc { boolean addFilters() default true; /** - * If {@link MvcResult} information should always be printed after each MockMVC - * invocation. Defaults to {@code true}. - * @return if result information is always printed + * How {@link MvcResult} information should be printed after each MockMVC invocation. + * @return how information is printed */ - boolean alwaysPrint() default true; + MockMvcPrint print() default MockMvcPrint.DEFAULT; /** * If a {@link WebClient} should be auto-configured when HtmlUnit is on the classpath. diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcPrint.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcPrint.java new file mode 100644 index 00000000000..efcc97bdced --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcPrint.java @@ -0,0 +1,55 @@ +/* + * 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.properties.UnmappedPropertyValue; + +/** + * MVC print options specified from {@link AutoConfigureMockMvc}. + * + * @author Phillip Webb + * @since 1.4.0 + */ +public enum MockMvcPrint { + + /** + * Use the default print setting ({@code MockMvcPrint.SYSTEM_OUT} unless explicitly + * overridden). + */ + @UnmappedPropertyValue DEFAULT, + + /** + * Log MVC interactions at the {@code DEBUG} level. + */ + LOG_DEBUG, + + /** + * Print MVC interactions to {@code System.out}. + */ + SYSTEM_OUT, + + /** + * Print MVC interactions to {@code System.err}. + */ + SYSTEM_ERR, + + /** + * Do not print MVC interactions. + */ + NONE + +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java index 4f942386d59..f8ff9f2b422 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java @@ -16,6 +16,7 @@ package org.springframework.boot.test.autoconfigure.web.servlet; +import java.io.PrintStream; import java.util.Collection; import javax.servlet.Filter; @@ -24,9 +25,12 @@ import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBea import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.servlet.ServletContextInitializerBeans; +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; +import org.springframework.util.CollectionUtils; import org.springframework.web.context.WebApplicationContext; /** @@ -44,7 +48,7 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi private boolean addFilters = true; - private boolean alwaysPrint = true; + private MockMvcPrint print = MockMvcPrint.DEFAULT; /** * Create a new {@link SpringBootMockMvcBuilderCustomizer} instance. @@ -60,11 +64,22 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi if (this.addFilters) { addFilters(builder); } - if (this.alwaysPrint) { - builder.alwaysDo(MockMvcResultHandlers.print(System.out)); + ResultHandler printHandler = getPrintHandler(); + if (printHandler != null) { + builder.alwaysDo(printHandler); } } + private ResultHandler getPrintHandler() { + if (this.print == MockMvcPrint.NONE) { + return null; + } + if (this.print == MockMvcPrint.LOG_DEBUG) { + return MockMvcResultHandlers.log(); + } + return new SystemResultHandler(this.print); + } + private void addFilters(ConfigurableMockMvcBuilder builder) { ServletContextInitializerBeans Initializers = new ServletContextInitializerBeans( this.context); @@ -106,12 +121,57 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi return this.addFilters; } - public void setAlwaysPrint(boolean alwaysPrint) { - this.alwaysPrint = alwaysPrint; + public void setPrint(MockMvcPrint print) { + this.print = print; } - public boolean isAlwaysPrint() { - return this.alwaysPrint; + public MockMvcPrint getPrint() { + return this.print; + } + + /** + * {@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. + */ + private static class SystemResultHandler extends PrintingResultHandler { + + protected SystemResultHandler(MockMvcPrint print) { + super(new SystemResultValuePrinter(print)); + } + + private static class SystemResultValuePrinter implements ResultValuePrinter { + + private final MockMvcPrint print; + + SystemResultValuePrinter(MockMvcPrint print) { + this.print = print; + } + + @Override + public void printHeading(String heading) { + getWriter().println(); + getWriter().println(String.format("%s:", heading)); + } + + @Override + public void printValue(String label, Object value) { + if (value != null && value.getClass().isArray()) { + value = CollectionUtils.arrayToList(value); + } + getWriter().println(String.format("%17s = %s", label, value)); + } + + private PrintStream getWriter() { + if (this.print == MockMvcPrint.SYSTEM_ERR) { + return System.err; + } + return System.out; + } + + } + } } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcSpringBootTestIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcSpringBootTestIntegrationTests.java index 773fafa1a39..57f2267b175 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcSpringBootTestIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcSpringBootTestIntegrationTests.java @@ -16,12 +16,14 @@ 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.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.rule.OutputCapture; import org.springframework.context.ApplicationContext; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; @@ -40,10 +42,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. */ @RunWith(SpringRunner.class) @SpringBootTest -@AutoConfigureMockMvc(alwaysPrint = false) +@AutoConfigureMockMvc(print = MockMvcPrint.SYSTEM_ERR) @WithMockUser(username = "user", password = "secret") public class MockMvcSpringBootTestIntegrationTests { + @Rule + public OutputCapture output = new OutputCapture(); + @MockBean private ExampleMockableService service; @@ -57,6 +62,7 @@ public class MockMvcSpringBootTestIntegrationTests { public void shouldFindController1() throws Exception { this.mvc.perform(get("/one")).andExpect(content().string("one")) .andExpect(status().isOk()); + assertThat(this.output.toString()).contains("Request URI = /one"); } @Test diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintDefaultIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintDefaultIntegrationTests.java new file mode 100644 index 00000000000..89a90c2d76d --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintDefaultIntegrationTests.java @@ -0,0 +1,55 @@ +/* + * 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(secure = false) +public class WebMvcTestPrintDefaultIntegrationTests { + + @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"); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintDefaultOverrideIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintDefaultOverrideIntegrationTests.java new file mode 100644 index 00000000000..edc1f58fe18 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintDefaultOverrideIntegrationTests.java @@ -0,0 +1,57 @@ +/* + * 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.TestPropertySource; +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} when a specific controller is defined. + * + * @author Phillip Webb + */ +@RunWith(SpringRunner.class) +@WebMvcTest(secure = false) +@TestPropertySource(properties = "spring.test.mockmvc.print=NONE") +public class WebMvcTestPrintDefaultOverrideIntegrationTests { + + @Rule + public OutputCapture output = new OutputCapture(); + + @Autowired + private MockMvc mvc; + + @Test + public void shouldFindController1() throws Exception { + this.mvc.perform(get("/one")).andExpect(content().string("one")) + .andExpect(status().isOk()); + assertThat(this.output.toString()).doesNotContain("Request URI = /one"); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintOverrideIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintOverrideIntegrationTests.java new file mode 100644 index 00000000000..79c3d146935 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestPrintOverrideIntegrationTests.java @@ -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} when a specific print option is defined. + * + * @author Phillip Webb + */ +@RunWith(SpringRunner.class) +@WebMvcTest +@AutoConfigureMockMvc(secure = false, print = MockMvcPrint.NONE) +public class WebMvcTestPrintOverrideIntegrationTests { + + @Rule + public OutputCapture output = new OutputCapture(); + + @Autowired + private MockMvc mvc; + + @Test + public void shouldNotPrint() throws Exception { + this.mvc.perform(get("/one")).andExpect(content().string("one")) + .andExpect(status().isOk()); + assertThat(this.output.toString()).doesNotContain("Request URI = /one"); + } + +}