Commit 4a1e0d45 authored by Phillip Webb's avatar Phillip Webb

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
parent 8355d851
...@@ -41,6 +41,7 @@ import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint; ...@@ -41,6 +41,7 @@ import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; 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.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint;
import org.springframework.boot.test.context.SpringBootContextLoader; import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -66,7 +67,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -66,7 +67,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
"endpoints.health.sensitive=true", "endpoints.actuator.enabled=false" }) "endpoints.health.sensitive=true", "endpoints.actuator.enabled=false" })
@DirtiesContext @DirtiesContext
@AutoConfigureRestDocs(EndpointDocumentation.RESTDOCS_OUTPUT_DIR) @AutoConfigureRestDocs(EndpointDocumentation.RESTDOCS_OUTPUT_DIR)
@AutoConfigureMockMvc(alwaysPrint = false) @AutoConfigureMockMvc(print = MockMvcPrint.NONE)
public class EndpointDocumentation { public class EndpointDocumentation {
static final String RESTDOCS_OUTPUT_DIR = "target/generated-snippets"; static final String RESTDOCS_OUTPUT_DIR = "target/generated-snippets";
......
...@@ -54,11 +54,10 @@ public @interface AutoConfigureMockMvc { ...@@ -54,11 +54,10 @@ public @interface AutoConfigureMockMvc {
boolean addFilters() default true; boolean addFilters() default true;
/** /**
* If {@link MvcResult} information should always be printed after each MockMVC * How {@link MvcResult} information should be printed after each MockMVC invocation.
* invocation. Defaults to {@code true}. * @return how information is printed
* @return if result information is always printed
*/ */
boolean alwaysPrint() default true; MockMvcPrint print() default MockMvcPrint.DEFAULT;
/** /**
* If a {@link WebClient} should be auto-configured when HtmlUnit is on the classpath. * If a {@link WebClient} should be auto-configured when HtmlUnit is on the classpath.
......
/*
* 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
}
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.test.autoconfigure.web.servlet; package org.springframework.boot.test.autoconfigure.web.servlet;
import java.io.PrintStream;
import java.util.Collection; import java.util.Collection;
import javax.servlet.Filter; import javax.servlet.Filter;
...@@ -24,9 +25,12 @@ import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBea ...@@ -24,9 +25,12 @@ import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBea
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletContextInitializerBeans; 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.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.PrintingResultHandler;
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
/** /**
...@@ -44,7 +48,7 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi ...@@ -44,7 +48,7 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
private boolean addFilters = true; private boolean addFilters = true;
private boolean alwaysPrint = true; private MockMvcPrint print = MockMvcPrint.DEFAULT;
/** /**
* Create a new {@link SpringBootMockMvcBuilderCustomizer} instance. * Create a new {@link SpringBootMockMvcBuilderCustomizer} instance.
...@@ -60,11 +64,22 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi ...@@ -60,11 +64,22 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
if (this.addFilters) { if (this.addFilters) {
addFilters(builder); addFilters(builder);
} }
if (this.alwaysPrint) { ResultHandler printHandler = getPrintHandler();
builder.alwaysDo(MockMvcResultHandlers.print(System.out)); 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) { private void addFilters(ConfigurableMockMvcBuilder<?> builder) {
ServletContextInitializerBeans Initializers = new ServletContextInitializerBeans( ServletContextInitializerBeans Initializers = new ServletContextInitializerBeans(
this.context); this.context);
...@@ -106,12 +121,57 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi ...@@ -106,12 +121,57 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
return this.addFilters; return this.addFilters;
} }
public void setAlwaysPrint(boolean alwaysPrint) { public void setPrint(MockMvcPrint print) {
this.alwaysPrint = alwaysPrint; this.print = print;
}
public MockMvcPrint getPrint() {
return this.print;
} }
public boolean isAlwaysPrint() { /**
return this.alwaysPrint; * {@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;
}
}
} }
} }
...@@ -16,12 +16,14 @@ ...@@ -16,12 +16,14 @@
package org.springframework.boot.test.autoconfigure.web.servlet; package org.springframework.boot.test.autoconfigure.web.servlet;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
...@@ -40,10 +42,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. ...@@ -40,10 +42,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc(alwaysPrint = false) @AutoConfigureMockMvc(print = MockMvcPrint.SYSTEM_ERR)
@WithMockUser(username = "user", password = "secret") @WithMockUser(username = "user", password = "secret")
public class MockMvcSpringBootTestIntegrationTests { public class MockMvcSpringBootTestIntegrationTests {
@Rule
public OutputCapture output = new OutputCapture();
@MockBean @MockBean
private ExampleMockableService service; private ExampleMockableService service;
...@@ -57,6 +62,7 @@ public class MockMvcSpringBootTestIntegrationTests { ...@@ -57,6 +62,7 @@ public class MockMvcSpringBootTestIntegrationTests {
public void shouldFindController1() throws Exception { public void shouldFindController1() throws Exception {
this.mvc.perform(get("/one")).andExpect(content().string("one")) this.mvc.perform(get("/one")).andExpect(content().string("one"))
.andExpect(status().isOk()); .andExpect(status().isOk());
assertThat(this.output.toString()).contains("Request URI = /one");
} }
@Test @Test
......
/*
* 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");
}
}
/*
* 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");
}
}
/*
* 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");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment