类 OutputCaptureExtension
java.lang.Object
org.springframework.boot.test.system.OutputCaptureExtension
- 所有已实现的接口:
org.junit.jupiter.api.extension.AfterAllCallback
,org.junit.jupiter.api.extension.AfterEachCallback
,org.junit.jupiter.api.extension.BeforeAllCallback
,org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
,org.junit.jupiter.api.extension.ParameterResolver
public class OutputCaptureExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.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>
- 从以下版本开始:
- 2.2.0
- 作者:
- Madhura Bhave, Phillip Webb, Andy Wilkinson, Sam Brannen
- 另请参阅:
-
方法概要
修饰符和类型方法说明void
afterAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
afterEach
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeAll
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) resolveParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) boolean
supportsParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
-
方法详细资料
-
beforeAll
- 指定者:
beforeAll
在接口中org.junit.jupiter.api.extension.BeforeAllCallback
- 抛出:
Exception
-
afterAll
- 指定者:
afterAll
在接口中org.junit.jupiter.api.extension.AfterAllCallback
- 抛出:
Exception
-
beforeEach
- 指定者:
beforeEach
在接口中org.junit.jupiter.api.extension.BeforeEachCallback
- 抛出:
Exception
-
afterEach
- 指定者:
afterEach
在接口中org.junit.jupiter.api.extension.AfterEachCallback
- 抛出:
Exception
-
supportsParameter
public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException - 指定者:
supportsParameter
在接口中org.junit.jupiter.api.extension.ParameterResolver
- 抛出:
org.junit.jupiter.api.extension.ParameterResolutionException
-
resolveParameter
public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) - 指定者:
resolveParameter
在接口中org.junit.jupiter.api.extension.ParameterResolver
-