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 InputStreamprivate final TextFromStandardInputStream.SystemInMock -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafter()protected voidbefore()static TextFromStandardInputStreamprivate Stringprivate StringvoidprovideLines(String... lines) Set the lines that are returned bySystem.in.voidprovideText(String... texts) Deprecated.please useprovideLines(String...)voidthrowExceptionOnInputEnd(IOException exception) Specify anIOExceptionthat is thrown bySystem.in.voidthrowExceptionOnInputEnd(RuntimeException exception) Specify aRuntimeExceptionthat 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 anIOExceptionthat 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- theIOExceptionthat is thrown.- Throws:
IllegalStateException- ifthrowExceptionOnInputEnd(RuntimeException)has been called before.- See Also:
-
throwExceptionOnInputEnd
Specify aRuntimeExceptionthat 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- theRuntimeExceptionthat is thrown.- Throws:
IllegalStateException- ifthrowExceptionOnInputEnd(IOException)has been called before.- See Also:
-
join
-
joinLines
-
before
- Overrides:
beforein classorg.junit.rules.ExternalResource- Throws:
Throwable
-
after
protected void after()- Overrides:
afterin classorg.junit.rules.ExternalResource
-
provideLines(String...)