Cleared deferred lines after each test

Previously, DeferredLinesWriter would collect MockMvc output from
every test that has executed. If a test eventually failed, the
output from every test up to including the one that had failed would
be output, rather than just the output for the test that has just
failed.

This commit clears the deferred lines after each test, thereby
ensuring that when a failure occurs only the lines from the failing
test are output.

Fixes gh-17551
This commit is contained in:
Andy Wilkinson
2019-07-17 12:26:58 +01:00
parent a64317b114
commit 9dabc0e14d
4 changed files with 29 additions and 7 deletions

View File

@@ -36,11 +36,12 @@ class MockMvcPrintOnlyOnFailureTestExecutionListener extends AbstractTestExecuti
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
if (testContext.getTestException() != null) {
DeferredLinesWriter writer = DeferredLinesWriter.get(testContext.getApplicationContext());
if (writer != null) {
DeferredLinesWriter writer = DeferredLinesWriter.get(testContext.getApplicationContext());
if (writer != null) {
if (testContext.getTestException() != null) {
writer.writeDeferredResult();
}
writer.clear();
}
}

View File

@@ -253,6 +253,10 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
}
}
void clear() {
this.lines.clear();
}
}
/**

View File

@@ -16,8 +16,10 @@
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@@ -38,6 +40,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WebMvcTest
@WithMockUser
@AutoConfigureMockMvc
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class WebMvcTestPrintDefaultIntegrationTests {
@Autowired

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
import org.assertj.core.matcher.AssertionMatcher;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
@@ -23,8 +24,7 @@ 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;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test runner used for {@link WebMvcTestPrintDefaultIntegrationTests}.
@@ -43,10 +43,24 @@ public class WebMvcTestPrintDefaultRunner extends SpringJUnit4ClassRunner {
statement = new AlwaysPassStatement(statement);
OutputCapture outputCapture = new OutputCapture();
if (frameworkMethod.getName().equals("shouldPrint")) {
outputCapture.expect(containsString("HTTP Method"));
outputCapture.expect(new AssertionMatcher<String>() {
@Override
public void assertion(String actual) throws AssertionError {
assertThat(actual).containsOnlyOnce("HTTP Method");
}
});
}
else if (frameworkMethod.getName().equals("shouldNotPrint")) {
outputCapture.expect(not(containsString("HTTP Method")));
outputCapture.expect(new AssertionMatcher<String>() {
@Override
public void assertion(String actual) throws AssertionError {
assertThat(actual).doesNotContain("HTTP Method");
}
});
}
else {
throw new IllegalStateException("Unexpected test method");