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
This commit is contained in:
Phillip Webb
2016-09-18 21:58:09 -07:00
parent 7ec14774a8
commit e239e64cb1
8 changed files with 368 additions and 42 deletions

View File

@@ -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 {

View File

@@ -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");
}
}

View File

@@ -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());
}
}

View File

@@ -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) {
}
}
}
}