public class ConsoleRedirectRule extends Object implements org.junit.rules.TestRule
ConsoleRedirectRule.
public class YourTest{
@Rule
private ConsoleRedirectRule consoleRedirectRule = ConsoleRedirectRule.instance();
public void yourTestMethod(){
System.out.println("HalloWelt");
assertThat(consoleRedirectRule.getOutput(), containsString("HalloWelt"));
assertThat(consoleRedirectRule.getOutput(), not("HalloWelt"));
}
public void yourSecondTestMethod(){
System.out.print("HalloWelt");
assertThat(consoleRedirectRule.getOutput(), is("HalloWelt"));
}
}
| Constructor and Description |
|---|
ConsoleRedirectRule() |
| Modifier and Type | Method and Description |
|---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement baseStatement,
org.junit.runner.Description description) |
String |
getOutput()
Returns the Output.
|
static ConsoleRedirectRule |
instance() |
public static ConsoleRedirectRule instance()
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement baseStatement,
org.junit.runner.Description description)
apply in interface org.junit.rules.TestRulepublic String getOutput()
Note:
If the SUT make System.out.println(...), the output contains the linefeeds.
Copyright © 2016. All rights reserved.