|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.junit.rules.ExternalResource
org.junit.contrib.java.lang.system.TextFromStandardInputStream
public class TextFromStandardInputStream
The 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 Summary | |
---|---|
TextFromStandardInputStream(String text)
Deprecated. use provideLines(String...) |
Method Summary | |
---|---|
protected void |
after()
|
protected void |
before()
|
static TextFromStandardInputStream |
emptyStandardInputStream()
|
void |
provideLines(String... lines)
Set the lines that are returned by System.in . |
void |
provideText(String... texts)
Deprecated. please use provideLines(String...) |
void |
throwExceptionOnInputEnd(IOException exception)
Specify an IOException that is thrown by System.in . |
void |
throwExceptionOnInputEnd(RuntimeException exception)
Specify a RuntimeException that is thrown by System.in . |
Methods inherited from class org.junit.rules.ExternalResource |
---|
apply |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
@Deprecated public TextFromStandardInputStream(String text)
provideLines(String...)
TextFromStandardInputStream
, which provides the
specified text.
text
- this text is return by System.in
.Method Detail |
---|
public static TextFromStandardInputStream emptyStandardInputStream()
@Deprecated public void provideText(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(String... lines)
System.in
.
System.getProperty("line.separator")
is used for the end
of line.
lines
- a list of lines.public void throwExceptionOnInputEnd(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.
IllegalStateException
- if
throwExceptionOnInputEnd(RuntimeException)
has been called before.throwExceptionOnInputEnd(RuntimeException)
public void throwExceptionOnInputEnd(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.
IllegalStateException
- if
throwExceptionOnInputEnd(IOException)
has been called before.throwExceptionOnInputEnd(IOException)
protected void before() throws Throwable
before
in class org.junit.rules.ExternalResource
Throwable
protected void after()
after
in class org.junit.rules.ExternalResource
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |