- java.lang.Object
-
- org.junit.rules.TestWatcher
-
- net.morimekta.testing.junit4.ConsoleWatcher
-
- All Implemented Interfaces:
org.junit.rules.TestRule
public class ConsoleWatcher extends org.junit.rules.TestWatcher
Printed output watcher rule.
Note that this watcher hijacks the defaultpublic class Test { {@literal@}Rule public ConsoleWatcher watcher = new ConsoleWatcher(); {@literal@}Test public void testOutput() { System.err.println("woot!"); assertThat(console.console().error(), is("woot!\n")); } }
System.in
,System.out
andSystem.err
streams. This may cause problems when other systems prints out to those streams and expects the output to be present for others. In some cases maven may balk at this, but it seems to pass silently (and successfully) in other cases.
-
-
Constructor Summary
Constructors Constructor Description ConsoleWatcher()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Console
console()
ConsoleWatcher
dumpErrorOnFailure()
Dump stderr to error output on failure.ConsoleWatcher
dumpOnFailure()
Dump both stdout and stderr to error output on failure.ConsoleWatcher
dumpOutputOnFailure()
Dump stdout to error output on failure.protected void
failed(Throwable e, org.junit.runner.Description description)
protected void
finished(org.junit.runner.Description description)
ConsoleWatcher
interactive()
Set input mode to interactive.ConsoleWatcher
nonInteractive()
Set input mode to non-interactive.protected void
starting(org.junit.runner.Description description)
net.morimekta.io.tty.TTY
tty()
ConsoleWatcher
withTerminalSize(int rows, int cols)
Set the current terminal size.
-
-
-
Method Detail
-
tty
public net.morimekta.io.tty.TTY tty()
- Returns:
- The testing TTY
-
console
public Console console()
- Returns:
- The testing console.
-
withTerminalSize
public ConsoleWatcher withTerminalSize(int rows, int cols)
Set the current terminal size.- Parameters:
rows
- Row count.cols
- Column count.- Returns:
- The console watcher.
-
nonInteractive
public ConsoleWatcher nonInteractive()
Set input mode to non-interactive. This makes the terminal no longer behave like an interactive terminal (the default for ConsoleWatcher), but as a wrapped shell script.- Returns:
- The console watcher.
-
interactive
public ConsoleWatcher interactive()
Set input mode to interactive. This makes the terminal behave like an interactive terminal (the default for ConsoleWatcher).- Returns:
- The console watcher.
-
dumpOutputOnFailure
public ConsoleWatcher dumpOutputOnFailure()
Dump stdout to error output on failure.- Returns:
- The console watcher.
-
dumpErrorOnFailure
public ConsoleWatcher dumpErrorOnFailure()
Dump stderr to error output on failure.- Returns:
- The console watcher.
-
dumpOnFailure
public ConsoleWatcher dumpOnFailure()
Dump both stdout and stderr to error output on failure.- Returns:
- The console watcher.
-
starting
protected void starting(org.junit.runner.Description description)
- Overrides:
starting
in classorg.junit.rules.TestWatcher
-
failed
protected void failed(Throwable e, org.junit.runner.Description description)
- Overrides:
failed
in classorg.junit.rules.TestWatcher
-
finished
protected void finished(org.junit.runner.Description description)
- Overrides:
finished
in classorg.junit.rules.TestWatcher
-
-