public final class CapturedOutput extends Object implements org.junit.rules.TestRule
System.out and System.err.
This rule redirects the constant print streams for out/err to an CapturingPrintStream before each test and
backups the origin streams after each test.
public class OutputTest {
@Rule
public final CapturedOutput output = new CapturedOutput();
@Test
public void captureOut() {
output.expectOut("foobar");
output.expectOut(not("snafu"));
System.out.print("foobar");
}
@Test
public void captureErr() {
output.expectErr("foobar");
output.expectErr(not("snafu"));
System.err.print("foobar");
}
}
| Modifier and Type | Class and Description |
|---|---|
private static class |
CapturedOutput.CapturedOutputMatcherBuilder
Builds string matchers.
|
| Modifier and Type | Field and Description |
|---|---|
private CapturingPrintStream |
err
Captures the data written to STDERR.
|
private PrintStream |
errBackup
Holds the original STDERR from before test method.
|
private CapturedOutput.CapturedOutputMatcherBuilder |
errMatcherBuilder
Build the matchers for STDERR expectations.
|
private CapturingPrintStream |
out
Captures the data written to STDOUT.
|
private PrintStream |
outBackup
Holds the original STDOUT from before test method.
|
private CapturedOutput.CapturedOutputMatcherBuilder |
outMatcherBuilder
Build the matchers for STDOUT expectations.
|
| Constructor and Description |
|---|
CapturedOutput()
Creates a capturing rule with platform encoding.
|
CapturedOutput(String encoding)
Dedicated constructor.
|
| Modifier and Type | Method and Description |
|---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
Description description) |
private void |
assertCapturedErr()
Applies matchers on captured error output if there are any matchers.
|
private void |
assertCapturedOut()
Applies matchers on captured standard output if there are any matchers.
|
void |
expectErr(org.hamcrest.Matcher<String> matcher)
Adds to the list of requirements for any output printed to STDERR.
|
void |
expectErr(String substring)
Adds to the list of requirements for any output printed to STDERR that it should contain string
substring. |
void |
expectOut(org.hamcrest.Matcher<String> matcher)
Adds to the list of requirements for any output printed to STDOUT.
|
void |
expectOut(String substring)
Adds to the list of requirements for any output printed to STDOUT that it should contain string
substring. |
private static <T> T |
notNull(T subject,
String description)
Validates that given subject is not
null. |
private void |
redirectOutputStreams()
Set and backup STDERR/STDOUT print streams.
|
private void |
restoreOutputStreams()
Restore STDERR/STDOUT print streams.
|
private final CapturedOutput.CapturedOutputMatcherBuilder outMatcherBuilder
private final CapturedOutput.CapturedOutputMatcherBuilder errMatcherBuilder
private final CapturingPrintStream out
private final CapturingPrintStream err
private PrintStream outBackup
private PrintStream errBackup
public CapturedOutput()
throws UnsupportedEncodingException
UnsupportedEncodingException - if the platform encoding is not supportedpublic CapturedOutput(String encoding) throws UnsupportedEncodingException
encoding - must not be null or emptyUnsupportedEncodingException - if the platform encoding is not supportedpublic void expectOut(String substring)
substring.substring - must not be nullpublic void expectOut(org.hamcrest.Matcher<String> matcher)
matcher - must not be nullpublic void expectErr(String substring)
substring.substring - must not be nullpublic void expectErr(org.hamcrest.Matcher<String> matcher)
matcher - must not be nullpublic org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, Description description)
apply in interface org.junit.rules.TestRuleprivate void redirectOutputStreams()
private void assertCapturedErr()
throws UnsupportedEncodingException
UnsupportedEncodingException - if the platform encoding is not supportedprivate void assertCapturedOut()
throws UnsupportedEncodingException
UnsupportedEncodingException - if the platform encoding is not supportedprivate void restoreOutputStreams()
private static <T> T notNull(T subject,
String description)
null.
Will throw NullPointerException if subject is null.
T - type of subjectsubject - tested subjectdescription - name of tested subject for exception messagenullCopyright © 2012 Sven Strittmatter. All Rights Reserved.