Add unit tests

This commit is contained in:
anujgaud 2024-02-03 19:17:45 +05:30 committed by GitHub
parent c651b85f54
commit d45bf19622
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 41 additions and 0 deletions

View File

@ -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());
}
}