diff --git a/src/main/java/BordComputer.java b/src/main/java/BordComputer.java index 21f6b38..156e191 100644 --- a/src/main/java/BordComputer.java +++ b/src/main/java/BordComputer.java @@ -4,6 +4,7 @@ import device.cdPlayer.CDPlayer; import device.radioPlayer.RadioPlayer; import device.usbPlayer.USB_Stick; import device.usbPlayer.UsbPlayer; +import exception.NoItemEvenAllPreconditionAreMet; import java.io.FileReader; import java.lang.reflect.InvocationTargetException; @@ -42,7 +43,13 @@ public class BordComputer { } public String play(){ - String itemToPlay=activeDevice.play(); + String itemToPlay=null; + try { + itemToPlay=activeDevice.play(); + }catch (RuntimeException exception) { + throw new NoItemEvenAllPreconditionAreMet(); + } + return itemToPlay; } diff --git a/src/main/java/exception/NoItemEvenAllPreconditionAreMet.java b/src/main/java/exception/NoItemEvenAllPreconditionAreMet.java new file mode 100644 index 0000000..ec1be23 --- /dev/null +++ b/src/main/java/exception/NoItemEvenAllPreconditionAreMet.java @@ -0,0 +1,7 @@ +package exception; + +public class NoItemEvenAllPreconditionAreMet extends RuntimeException{ + public NoItemEvenAllPreconditionAreMet(){ + super("No Item returned even all preconditions are met."); + } +} diff --git a/src/test/java/BordComputerTest.java b/src/test/java/BordComputerTest.java index 31689aa..729d591 100644 --- a/src/test/java/BordComputerTest.java +++ b/src/test/java/BordComputerTest.java @@ -1,7 +1,9 @@ import device.Device; import device.cdPlayer.CDPlayer; +import device.cdPlayer.exceptions.NegativeInputException; import device.radioPlayer.RadioPlayer; import device.usbPlayer.UsbPlayer; +import exception.NoItemEvenAllPreconditionAreMet; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -100,15 +102,25 @@ class BordComputerTest { bordComputer.meetAllPreconditionForPlay(); assertThat(bordComputer.play()).describedAs(testName).isNotEqualTo(null); } + if(testCase.equals("Case4")) { + Exception newException = assertThrows(NoItemEvenAllPreconditionAreMet.class, () -> bordComputer.play()); + assertEquals(NoItemEvenAllPreconditionAreMet.class, newException.getClass()); + assertEquals(newException.getMessage(), "No Item returned even all preconditions are met."); + } } static Stream playDate()throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { BordComputer bordComputer = new BordComputer(); - bordComputer.readConfig(); + bordComputer.readConfig(); + + BordComputer bordComputer2 = new BordComputer(); + bordComputer2.readConfig(); + return Stream.of( Arguments.of("[CDPlayer-> play(): after all preconditions are met] => ShouldReturnAnItemToPlay","Case1",bordComputer,"CDPlayer"), Arguments.of("[RadioPlayer-> play(): after all preconditions are met] => ShouldReturnAnItemToPlay","Case2",bordComputer,"RadioPlayer"), - Arguments.of("[UsbPlayer-> play(): after all preconditions are met] => ShouldReturnAnItemToPlay","Case3",bordComputer,"UsbPlayer") + Arguments.of("[UsbPlayer-> play(): after all preconditions are met] => ShouldReturnAnItemToPlay","Case3",bordComputer,"UsbPlayer"), + Arguments.of("[CDPlayer-> play(): without fulfilling preconditions] => ShouldReturnAnException","Case4",bordComputer2,"CDPlayer") ); }