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
This commit is contained in:
parent
8355d8516b
commit
4a1e0d4544
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
spring.test.mockmvc.print=none
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
@ -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 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean isAlwaysPrint() {
|
||||
return this.alwaysPrint;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue