org.junit.contrib.java.lang.system
Class TextFromStandardInputStream

java.lang.Object
  extended by org.junit.rules.ExternalResource
      extended by 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.


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

TextFromStandardInputStream

@Deprecated
public TextFromStandardInputStream(String text)
Deprecated. use provideLines(String...)

Create a new TextFromStandardInputStream, which provides the specified text.

Parameters:
text - this text is return by System.in.
Method Detail

emptyStandardInputStream

public static TextFromStandardInputStream emptyStandardInputStream()

provideText

@Deprecated
public void provideText(String... texts)
Deprecated. please use provideLines(String...)

Set the text that is returned by System.in. You can provide multiple texts. In that case the texts are concatenated.

Parameters:
texts - a list of texts.

provideLines

public void provideLines(String... lines)
Set the lines that are returned by System.in. System.getProperty("line.separator") is used for the end of line.

Parameters:
lines - a list of lines.

throwExceptionOnInputEnd

public void throwExceptionOnInputEnd(IOException exception)
Specify an 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.

Parameters:
exception - the IOException that is thrown.
Throws:
IllegalStateException - if throwExceptionOnInputEnd(RuntimeException) has been called before.
See Also:
throwExceptionOnInputEnd(RuntimeException)

throwExceptionOnInputEnd

public void throwExceptionOnInputEnd(RuntimeException exception)
Specify a 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.

Parameters:
exception - the RuntimeException that is thrown.
Throws:
IllegalStateException - if throwExceptionOnInputEnd(IOException) has been called before.
See Also:
throwExceptionOnInputEnd(IOException)

before

protected void before()
               throws Throwable
Overrides:
before in class org.junit.rules.ExternalResource
Throws:
Throwable

after

protected void after()
Overrides:
after in class org.junit.rules.ExternalResource


Copyright © 2011–2018. All rights reserved.