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-6653pull/6707/merge
parent
7ec14774a8
commit
e239e64cb1
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
|
||||
/**
|
||||
* {@link TestExecutionListener} used to print MVC lines only on failure.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class MockMvcPrintOnlyOnFailureTestExecutionListener
|
||||
extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
if (testContext.getTestException() != null) {
|
||||
DeferredLinesWriter writer = DeferredLinesWriter
|
||||
.get(testContext.getApplicationContext());
|
||||
if (writer != null) {
|
||||
writer.writeDeferredResult();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
@ -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) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue