Class TextFromStandardInputStream
java.lang.Object
org.junit.rules.ExternalResource
org.junit.contrib.java.lang.system.TextFromStandardInputStream
- All Implemented Interfaces:
org.junit.rules.TestRule
public class TextFromStandardInputStream
extends org.junit.rules.ExternalResource
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()); } }
Throwing Exceptions
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
.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate InputStream
private final TextFromStandardInputStream.SystemInMock
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
after()
protected void
before()
static TextFromStandardInputStream
private String
private String
void
provideLines
(String... lines) Set the lines that are returned bySystem.in
.void
provideText
(String... texts) Deprecated.please useprovideLines(String...)
void
throwExceptionOnInputEnd
(IOException exception) Specify anIOException
that is thrown bySystem.in
.void
throwExceptionOnInputEnd
(RuntimeException exception) Specify aRuntimeException
that is thrown bySystem.in
.Methods inherited from class org.junit.rules.ExternalResource
apply
-
Field Details
-
systemInMock
-
originalIn
-
-
Constructor Details
-
TextFromStandardInputStream
Deprecated.Create a newTextFromStandardInputStream
, which provides the specified text.- Parameters:
text
- this text is return bySystem.in
.
-
-
Method Details
-
emptyStandardInputStream
-
provideText
Deprecated.please useprovideLines(String...)
Set the text that is returned bySystem.in
. You can provide multiple texts. In that case the texts are concatenated.- Parameters:
texts
- a list of texts.
-
provideLines
Set the lines that are returned bySystem.in
.System.getProperty("line.separator")
is used for the end of line.- Parameters:
lines
- a list of lines.
-
throwExceptionOnInputEnd
Specify anIOException
that is thrown bySystem.in
. If you callprovideLines(String...)
orprovideText(String...)
in addition then the exception is thrown after the text has been read fromSystem.in
.- Parameters:
exception
- theIOException
that is thrown.- Throws:
IllegalStateException
- ifthrowExceptionOnInputEnd(RuntimeException)
has been called before.- See Also:
-
throwExceptionOnInputEnd
Specify aRuntimeException
that is thrown bySystem.in
. If you callprovideLines(String...)
orprovideText(String...)
in addition then the exception is thrown after the text has been read fromSystem.in
.- Parameters:
exception
- theRuntimeException
that is thrown.- Throws:
IllegalStateException
- ifthrowExceptionOnInputEnd(IOException)
has been called before.- See Also:
-
join
-
joinLines
-
before
- Overrides:
before
in classorg.junit.rules.ExternalResource
- Throws:
Throwable
-
after
protected void after()- Overrides:
after
in classorg.junit.rules.ExternalResource
-
provideLines(String...)