Class OutputCaptureExtension
java.lang.Object
org.springframework.boot.test.system.OutputCaptureExtension
- All Implemented Interfaces:
AfterAllCallback, AfterEachCallback, BeforeAllCallback, BeforeEachCallback, Extension, ParameterResolver, TestInstantiationAwareExtension
public class OutputCaptureExtension
extends Object
implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver
JUnit Jupiter
@Extension to capture System.out and
System.err. Can be registered for an entire test class or for an
individual test method through @ExtendWith. This extension provides
parameter resolution for a CapturedOutput
instance which can be used to assert that the correct output was written.
To use with @ExtendWith, inject the CapturedOutput as an
argument to your test class constructor, test method, or lifecycle methods:
@ExtendWith(OutputCaptureExtension.class)
class MyTest {
@Test
void test(CapturedOutput output) {
System.out.println("ok");
assertThat(output).contains("ok");
System.err.println("error");
}
@AfterEach
void after(CapturedOutput output) {
assertThat(output.getOut()).contains("ok");
assertThat(output.getErr()).contains("error");
}
}
To ensure that their output can be captured, Java Util Logging (JUL) and Log4j2 require additional configuration.
To reliably capture output from Java Util Logging, reset its configuration after each test:
@AfterEach
void reset() throws Exception {
LogManager.getLogManager().readConfiguration();
}
To reliably capture output from Log4j2, set the follow attribute of the
console appender to true:
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
...
</Console>
</Appenders>
- Since:
- 2.2.0
- Author:
- Madhura Bhave, Phillip Webb, Andy Wilkinson, Sam Brannen
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface TestInstantiationAwareExtension
TestInstantiationAwareExtension.ExtensionContextScope -
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(ExtensionContext context) voidafterEach(ExtensionContext context) voidbeforeAll(ExtensionContext context) voidbeforeEach(ExtensionContext context) resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Method Details
-
beforeAll
- Specified by:
beforeAllin interfaceBeforeAllCallback- Throws:
Exception
-
afterAll
- Specified by:
afterAllin interfaceAfterAllCallback- Throws:
Exception
-
beforeEach
- Specified by:
beforeEachin interfaceBeforeEachCallback- Throws:
Exception
-
afterEach
- Specified by:
afterEachin interfaceAfterEachCallback- Throws:
Exception
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException - Specified by:
supportsParameterin interfaceParameterResolver- Throws:
ParameterResolutionException
-
resolveParameter
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - Specified by:
resolveParameterin interfaceParameterResolver
-