From 9ee42e098f386800df194540ef7a4abcb9f113df Mon Sep 17 00:00:00 2001 From: KDippold <2023962@stud.hs-mannheim.de> Date: Sun, 8 Jan 2023 01:30:18 +0100 Subject: [PATCH] R2D2 Test und Roboter Test erweitert --- Roboterfabrik/src/Domäne/R2D2Test.java | 57 +++++++++++++++++++++++ Roboterfabrik/src/Domäne/RoboterTest.java | 33 ++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 Roboterfabrik/src/Domäne/R2D2Test.java diff --git a/Roboterfabrik/src/Domäne/R2D2Test.java b/Roboterfabrik/src/Domäne/R2D2Test.java new file mode 100644 index 0000000..5f10c9f --- /dev/null +++ b/Roboterfabrik/src/Domäne/R2D2Test.java @@ -0,0 +1,57 @@ +package Domäne; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import tpe.exceptions.roboter.exceptions.RobotException; +import tpe.exceptions.roboter.exceptions.RobotIllegalStateException; +import tpe.exceptions.roboter.exceptions.RobotMagicValueException; + +class R2D2Test { + + @Test + void getIdTest() { + R2D2 junit = new R2D2("junit", 1); + assertEquals(1, junit.getId()); + } + + @Test + void getRobotTypeTest() { + R2D2 junit = new R2D2("junit", 1); + assertEquals(RobotType.R2D2, junit.getRobotType()); + } + + @Test + void powerTest() { + R2D2 junit = new R2D2("junit", 1); + int[] arr = { 1, 3, 5 }; + assertThrows(RobotIllegalStateException.class, () -> junit.think(arr)); + } + + @Test + void magicValueTest() { + R2D2 junit = new R2D2("junit", 1); + junit.triggerPowerSwitch(); + int[] arr = { 1, 5, 42, 7, 15 }; + Throwable exception = assertThrows(RobotMagicValueException.class, () -> junit.think(arr)); + assertEquals("Fehler! Zahl 42 im Array!", exception.getMessage()); + } + + @Test + void thinkTest() { + R2D2 junit = new R2D2("junit", 1); + junit.triggerPowerSwitch(); + int[] arr = { 1, 5, 43, 7, 15 }; + int[] ziel = { 1, 5, 7, 15, 43 }; + try { + assertArrayEquals(ziel, junit.think(arr)); + } catch (RobotException e) { + e.printStackTrace(); + System.out.println("Zahlen im Array sind ungültig!"); + } + } + +} diff --git a/Roboterfabrik/src/Domäne/RoboterTest.java b/Roboterfabrik/src/Domäne/RoboterTest.java index 2c416c1..670f3ef 100644 --- a/Roboterfabrik/src/Domäne/RoboterTest.java +++ b/Roboterfabrik/src/Domäne/RoboterTest.java @@ -1,10 +1,17 @@ package Domäne; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; import tpe.exceptions.roboter.exceptions.RobotException; +import tpe.exceptions.roboter.exceptions.RobotIllegalStateException; +import tpe.exceptions.roboter.exceptions.RobotMagicValueException; class RoboterTest { @@ -56,4 +63,28 @@ class RoboterTest { assertFalse(pris.isPowerOn()); } + + @Test + void prepareAndSpeakTest() { + Roboter test1 = new R2D2("Test", 123); + Roboter test2 = new C3PO("Test2", 321); + int[] arr = { 1, 2, 3 }; + int[] douglas = { 1, 2, 42 }; + assertThrows(RobotIllegalStateException.class, () -> test1.speak(arr)); + test1.triggerPowerSwitch(); + test2.triggerPowerSwitch(); + Throwable exception = assertThrows(RobotMagicValueException.class, () -> test1.think(douglas)); + assertEquals("Fehler! Zahl 42 im Array!", exception.getMessage()); + + try { + assertEquals("1, 2, 3, ", test1.speak(arr)); + test2.think(arr); + assertEquals("3; 2; 1; ", test2.speak(arr)); + } catch (RobotException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + System.out.println("Zahlen im Array sind ungültig!"); + } + + } }