org.junit.rules.TestRule
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
.
Constructor | Description |
---|---|
TextFromStandardInputStream(java.lang.String text) |
Deprecated.
|
Modifier and Type | Method | Description |
---|---|---|
protected void |
after() |
|
protected void |
before() |
|
static TextFromStandardInputStream |
emptyStandardInputStream() |
|
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 . |
@Deprecated public TextFromStandardInputStream(java.lang.String text)
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)
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
Copyright © 2011–2018. All rights reserved.