From f9e4108705486cbe4473baff365e85d6f7439d4c Mon Sep 17 00:00:00 2001 From: lorenzo Date: Mon, 8 Apr 2024 12:29:39 +0200 Subject: [PATCH] Junits --- InputOutputException/.classpath | 1 + .../src/inputOutput/ConsoleTest.java | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 InputOutputException/src/inputOutput/ConsoleTest.java diff --git a/InputOutputException/.classpath b/InputOutputException/.classpath index 57bca72..dbd59da 100644 --- a/InputOutputException/.classpath +++ b/InputOutputException/.classpath @@ -6,5 +6,6 @@ + diff --git a/InputOutputException/src/inputOutput/ConsoleTest.java b/InputOutputException/src/inputOutput/ConsoleTest.java new file mode 100644 index 0000000..312e535 --- /dev/null +++ b/InputOutputException/src/inputOutput/ConsoleTest.java @@ -0,0 +1,40 @@ +package inputOutput; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import org.junit.Test; +import java.io.ByteArrayInputStream; + +public class ConsoleTest { + + @Test + public void testReadString() { + String input = "Test String"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + assertEquals(input, Console.readString()); + } + + @Test + public void testReadCharArray() { + String input = "Test Char Array"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + char[] expected = input.toCharArray(); + assertArrayEquals(expected, Console.readCharArray()); + } + + @Test + public void testReadBooleanTrue() { + String input = "true"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + assertTrue(Console.readBoolean()); + } + + @Test + public void testReadBooleanFalse() { + String input = "false"; + System.setIn(new ByteArrayInputStream(input.getBytes())); + assertFalse(Console.readBoolean()); + } +}