Commit 9dabc0e1 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent a64317b1
......@@ -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();
}
}
......
......@@ -253,6 +253,10 @@ public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomi
}
}
void clear() {
this.lines.clear();
}
}
/**
......
......@@ -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
......
......@@ -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");
......
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