Add unit tests
This commit is contained in:
parent
c651b85f54
commit
d45bf19622
|
@ -0,0 +1,41 @@
|
||||||
|
package com.baeldung.systemin;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class SystemInReadUnitTest {
|
||||||
|
@Test
|
||||||
|
void givenUserInput_whenUsingReadMultipleCharacters_thenRead() {
|
||||||
|
System.setIn(new ByteArrayInputStream("Hello\n".getBytes()));
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
System.setOut(new PrintStream(outputStream));
|
||||||
|
SystemInRead.readMultipleCharacters();
|
||||||
|
|
||||||
|
assertEquals("Enter characters (Press 'Enter' to quit):\n" + "Hello", outputStream.toString().trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenUserInput_whenUsingReadSingleCharacter_thenRead() {
|
||||||
|
System.setIn(new ByteArrayInputStream("A".getBytes()));
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
System.setOut(new PrintStream(outputStream));
|
||||||
|
SystemInRead.readSingleCharacter();
|
||||||
|
|
||||||
|
assertEquals("Enter a character:\nA", outputStream.toString().trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenUserInput_whenUsingReadSingleCharacter_thenReading() {
|
||||||
|
System.setIn(new ByteArrayInputStream("ABC".getBytes()));
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
System.setOut(new PrintStream(outputStream));
|
||||||
|
SystemInRead.readWithParameters();
|
||||||
|
|
||||||
|
assertEquals("Data read: ABC\n" + "Bytes Read: 3", outputStream.toString().trim());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue