public class TextFromStandardInputStream
extends org.junit.rules.ExternalResource
TextFromStandardInputStream
rule replaces System.in
with
another InputStream
, which provides an arbitrary text. The original
System.in
is restored after the test.
public void MyTest { @Rule public final TextFromStandardInputStream systemInMock = emptyStandardInputStream(); @Test public void readTextFromStandardInputStream() { systemInMock.provideLines("foo", "bar"); Scanner scanner = new Scanner(System.in); scanner.nextLine(); assertEquals("bar", scanner.nextLine()); } }
TextFromStandardInputStream
can also simulate a System.in
that throws an IOException
or RuntimeException
. Use
systemInMock.throwExceptionOnInputEnd(IOException)
or
systemInMock.throwExceptionOnInputEnd(RuntimeException)
If you call provideLines(String...)
in addition then the
exception is thrown after the text has been read from System.in
.
Modifier and Type | Class and Description |
---|---|
private static class |
TextFromStandardInputStream.SystemInMock |
Modifier and Type | Field and Description |
---|---|
private java.io.InputStream |
originalIn |
private TextFromStandardInputStream.SystemInMock |
systemInMock |
Constructor and Description |
---|
TextFromStandardInputStream(java.lang.String text)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
protected void |
after() |
protected void |
before() |
static TextFromStandardInputStream |
emptyStandardInputStream() |
private java.lang.String |
join(java.lang.String[] texts) |
private java.lang.String |
joinLines(java.lang.String[] lines) |
void |
provideLines(java.lang.String... lines)
Set the lines that are returned by
System.in . |
void |
provideText(java.lang.String... texts)
Deprecated.
please use
provideLines(String...) |
void |
throwExceptionOnInputEnd(java.io.IOException exception)
Specify an
IOException that is thrown by System.in . |
void |
throwExceptionOnInputEnd(java.lang.RuntimeException exception)
Specify a
RuntimeException that is thrown by System.in . |
private final TextFromStandardInputStream.SystemInMock systemInMock
private java.io.InputStream originalIn
@Deprecated public TextFromStandardInputStream(java.lang.String text)
provideLines(String...)
TextFromStandardInputStream
, which provides the
specified text.text
- this text is return by System.in
.public static TextFromStandardInputStream emptyStandardInputStream()
@Deprecated public void provideText(java.lang.String... texts)
provideLines(String...)
System.in
. You can
provide multiple texts. In that case the texts are concatenated.texts
- a list of texts.public void provideLines(java.lang.String... lines)
System.in
.
System.getProperty("line.separator")
is used for the end
of line.lines
- a list of lines.public void throwExceptionOnInputEnd(java.io.IOException exception)
IOException
that is thrown by System.in
. If
you call provideLines(String...)
or
provideText(String...)
in addition then the exception is thrown
after the text has been read from System.in
.exception
- the IOException
that is thrown.java.lang.IllegalStateException
- if
throwExceptionOnInputEnd(RuntimeException)
has been called before.throwExceptionOnInputEnd(RuntimeException)
public void throwExceptionOnInputEnd(java.lang.RuntimeException exception)
RuntimeException
that is thrown by System.in
.
If you call provideLines(String...)
or
provideText(String...)
in addition then the exception is thrown
after the text has been read from System.in
.exception
- the RuntimeException
that is thrown.java.lang.IllegalStateException
- if
throwExceptionOnInputEnd(IOException)
has been called before.throwExceptionOnInputEnd(IOException)
private java.lang.String join(java.lang.String[] texts)
private java.lang.String joinLines(java.lang.String[] lines)
protected void before() throws java.lang.Throwable
before
in class org.junit.rules.ExternalResource
java.lang.Throwable
protected void after()
after
in class org.junit.rules.ExternalResource