Package net.morimekta.testing.rules
Class ConsoleWatcher
- java.lang.Object
-
- org.junit.rules.TestWatcher
-
- net.morimekta.testing.rules.ConsoleWatcher
-
- All Implemented Interfaces:
java.lang.AutoCloseable,org.junit.rules.TestRule
public class ConsoleWatcher extends org.junit.rules.TestWatcher implements java.lang.AutoCloseablePrinted output watcher rule.
Note that this watcher hijacks the defaultpublic class Test { {@literal@}Rule public ConsoleWatcher out = new ConsoleWatcher(); {@literal@}Test public void testOutput() { System.err.println("woot!"); assertThat(out.error(), is("woot!\n")); } }System.in,System.outandSystem.errstreams. 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 Deprecated Methods Modifier and Type Method Description voidclose()ConsoleWatcherdumpErrorOnFailure()Dump stderr to error output on failure.ConsoleWatcherdumpOnFailure()Dump both stdout and stderr to error output on failure.ConsoleWatcherdumpOutputOnFailure()Dump stdout to error output on failure.java.lang.Stringerror()protected voidfailed(java.lang.Throwable e, org.junit.runner.Description description)protected voidfinished(org.junit.runner.Description description)ConsoleWatcherinteractive()Deprecated.ConsoleWatchernonInteractive()Set input mode to non-interactive.java.lang.Stringoutput()voidreset()Reset all the streams for the console.ConsoleWatchersetInput(byte[] in)Set input to return the given bytes.ConsoleWatchersetInput(java.lang.Object... in)Set input with dynamic content.protected voidskipped(org.junit.AssumptionViolatedException e, org.junit.runner.Description description)protected voidstarting(org.junit.runner.Description description)protected voidsucceeded(org.junit.runner.Description description)net.morimekta.console.util.STTYtty()ConsoleWatcherwithTerminalSize(int rows, int cols)Set the current terminal size.
-
-
-
Method Detail
-
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
@Deprecated public ConsoleWatcher interactive()
Deprecated.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.
-
reset
public void reset()
Reset all the streams for the console.
-
output
public java.lang.String output()
- Returns:
- Get the normal output.
-
error
public java.lang.String error()
- Returns:
- Get the error output.
-
setInput
public ConsoleWatcher setInput(@Nonnull byte[] in)
Set input to return the given bytes.- Parameters:
in- The bytes for input.- Returns:
- The console watcher.
-
setInput
public ConsoleWatcher setInput(java.lang.Object... in)
Set input with dynamic content.- Parameters:
in- The input values.- Returns:
- The console watcher.
-
tty
public net.morimekta.console.util.STTY tty()
- Returns:
- The testing TTY
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
starting
protected void starting(org.junit.runner.Description description)
- Overrides:
startingin classorg.junit.rules.TestWatcher
-
failed
protected void failed(java.lang.Throwable e, org.junit.runner.Description description)- Overrides:
failedin classorg.junit.rules.TestWatcher
-
succeeded
protected void succeeded(org.junit.runner.Description description)
- Overrides:
succeededin classorg.junit.rules.TestWatcher
-
skipped
protected void skipped(org.junit.AssumptionViolatedException e, org.junit.runner.Description description)- Overrides:
skippedin classorg.junit.rules.TestWatcher
-
finished
protected void finished(org.junit.runner.Description description)
- Overrides:
finishedin classorg.junit.rules.TestWatcher
-
-