In our application, we expect user input within a Thread
as follows :
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
I want to pass that part in my unit test so that I can resume the thread to execute the rest of the code. How can I write something into System.in
from junit?
System.in provides the input stream from the keyboard and InputStreamReader reads the stream. Next, a BufferedReader object is instantiated with the InputStreamReader object passed to the constructor.
in. read(byte[]) System is a class in java. lang package. in is a static data member in the system class of type input stream class.
System.in in java means to take input from the keyboard or user. System. out in java means to print the output to console.
System. in is an object of InputStream which receives the data in the form of bytes from the keyboard. Then the task of reading and decoding the bytes into characters is done by the InputStreamReader.
What you want to do is use the method setIn()
from System
. This will let you pass data into System.in
from junit.
Replace it for the duration of your test:
String data = "the text you want to send";
InputStream testInput = new ByteArrayInputStream( data.getBytes("UTF-8") );
InputStream old = System.in;
try {
System.setIn( testInput );
...
} finally {
System.setIn( old );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With