diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureExtension.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureExtension.java index 060827730924..7007d0a7414b 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureExtension.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureExtension.java @@ -22,6 +22,8 @@ import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; +import org.junit.jupiter.api.extension.ExtensionContext.Store; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolutionException; import org.junit.jupiter.api.extension.ParameterResolver; @@ -66,30 +68,28 @@ public class OutputCaptureExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver { - private final OutputCapture outputCapture = new OutputCapture(); - OutputCaptureExtension() { // Package private to prevent users from directly creating an instance. } @Override public void beforeAll(ExtensionContext context) throws Exception { - this.outputCapture.push(); + getOutputCapture(context).push(); } @Override public void afterAll(ExtensionContext context) throws Exception { - this.outputCapture.pop(); + getOutputCapture(context).pop(); } @Override public void beforeEach(ExtensionContext context) throws Exception { - this.outputCapture.push(); + getOutputCapture(context).push(); } @Override public void afterEach(ExtensionContext context) throws Exception { - this.outputCapture.pop(); + getOutputCapture(context).pop(); } @Override @@ -100,8 +100,16 @@ public boolean supportsParameter(ParameterContext parameterContext, @Override public Object resolveParameter(ParameterContext parameterContext, - ExtensionContext extensionContext) throws ParameterResolutionException { - return this.outputCapture; + ExtensionContext extensionContext) { + return getOutputCapture(extensionContext); + } + + private OutputCapture getOutputCapture(ExtensionContext context) { + return getStore(context).getOrComputeIfAbsent(OutputCapture.class); + } + + private Store getStore(ExtensionContext context) { + return context.getStore(Namespace.create(getClass())); } }