From 007aa5d41574bdaa3810772678d8dd693a708622 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Wed, 16 Feb 2022 19:45:38 +0100 Subject: [PATCH 1/5] Method getInfoText of CDPlayer tested on shouldThrowReturnValueNullException. --- src/main/java/device/cdPlayer/CDPlayer.java | 9 ++++++++- .../exceptions/ReturnValueNullException.java | 9 +++++++++ .../java/device/cdPlayer/CDPlayerTest.java | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/main/java/device/cdPlayer/exceptions/ReturnValueNullException.java diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index f5f93cc..c3d0ce1 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -14,6 +14,7 @@ public class CDPlayer implements Device { private int CdDriveContent=0; private int CdFlap=0; private int volume = 0; + private String infoText=null; private List supportedFormats = new ArrayList(Arrays.asList(new String[] {"Audio","MP3","WMA","AAC"})); private String actualPlayTrack=""; @@ -66,7 +67,10 @@ public class CDPlayer implements Device { @Override public String getInfoText() { - return null; + if(infoText==null){ + throw new ReturnValueNullException(); + } + return infoText; } @Override @@ -157,4 +161,7 @@ public class CDPlayer implements Device { return actualPlayTrack; } + public void setInfoText(String intoText) { + this.infoText=intoText; + } } diff --git a/src/main/java/device/cdPlayer/exceptions/ReturnValueNullException.java b/src/main/java/device/cdPlayer/exceptions/ReturnValueNullException.java new file mode 100644 index 0000000..15f8ca0 --- /dev/null +++ b/src/main/java/device/cdPlayer/exceptions/ReturnValueNullException.java @@ -0,0 +1,9 @@ +package device.cdPlayer.exceptions; + +public class ReturnValueNullException extends RuntimeException{ + + public ReturnValueNullException() { + super("Method should not return a Null-Value."); + } + +} diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 3bcc1db..eee04af 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import java.lang.reflect.Method; @@ -17,6 +18,8 @@ import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + @ExtendWith(MockitoExtension.class) class CDPlayerTest { @@ -272,4 +275,19 @@ class CDPlayerTest { assertThat(volumeIstInValueRange).isEqualTo(true); } + @ParameterizedTest + @MethodSource("getInfoTestData") + void getInfoTest(String testName, CDPlayer _cdPlayer,Exception exception , String expectedResult){ + Exception newException=assertThrows(exception.getClass(),()->_cdPlayer.getInfoText()); + assertEquals(ReturnValueNullException.class, newException.getClass()); + } + static Stream getInfoTestData () { + + CDPlayer cdPlayer1 = new CDPlayer(); + + return Stream.of( + Arguments.of("[getInfoText() by infoText=null ] => shouldThrowReturnValueNullException", cdPlayer1,new ReturnValueNullException(),"") + ); + } + } \ No newline at end of file From d606a41ef7dede0142ea21ee7a1b30287c2b1b76 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Wed, 16 Feb 2022 20:02:33 +0100 Subject: [PATCH 2/5] Method getInfoText of CDPlayer tested on ExceptionShouldReturnAMessage. --- src/test/java/device/cdPlayer/CDPlayerTest.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index eee04af..4e9e7c4 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -277,16 +277,22 @@ class CDPlayerTest { @ParameterizedTest @MethodSource("getInfoTestData") - void getInfoTest(String testName, CDPlayer _cdPlayer,Exception exception , String expectedResult){ - Exception newException=assertThrows(exception.getClass(),()->_cdPlayer.getInfoText()); - assertEquals(ReturnValueNullException.class, newException.getClass()); + void getInfoTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception , String expectedResult){ + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.getInfoText()); + if(cases.equals("Case1")) { + assertEquals(ReturnValueNullException.class, newException.getClass()); + } + if(cases.equals("Case2")) { + assertThat(newException.getMessage()).describedAs(testName).isEqualTo(expectedResult); + } } static Stream getInfoTestData () { CDPlayer cdPlayer1 = new CDPlayer(); return Stream.of( - Arguments.of("[getInfoText() by infoText=null ] => shouldThrowReturnValueNullException", cdPlayer1,new ReturnValueNullException(),"") + Arguments.of("[getInfoText() by infoText=null ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException(),""), + Arguments.of("[getInfoText() by infoText=null ] => ExceptionShouldReturnAMessage","Case2",cdPlayer1,new ReturnValueNullException(),"Method should not return a Null-Value.") ); } From 1f11a8366c9d699ca46bd24ccce796d2624c1e80 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Wed, 16 Feb 2022 21:26:22 +0100 Subject: [PATCH 3/5] Method getInfoText of CDPlayer tested on MessageShouldContainInfoOfActualPlayTrack. --- src/main/java/device/cdPlayer/CDPlayer.java | 6 +++-- .../java/device/cdPlayer/CDPlayerTest.java | 26 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index c3d0ce1..14ace41 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -161,7 +161,9 @@ public class CDPlayer implements Device { return actualPlayTrack; } - public void setInfoText(String intoText) { - this.infoText=intoText; + public void setInfoText(String infoText) { + this.infoText=infoText; } + + } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 4e9e7c4..e6b26e9 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -278,21 +278,41 @@ class CDPlayerTest { @ParameterizedTest @MethodSource("getInfoTestData") void getInfoTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception , String expectedResult){ - Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.getInfoText()); + if(cases.equals("Case1")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.getInfoText()); assertEquals(ReturnValueNullException.class, newException.getClass()); } if(cases.equals("Case2")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.getInfoText()); assertThat(newException.getMessage()).describedAs(testName).isEqualTo(expectedResult); } + if(cases.equals("Case3")) { + String[] album= _cdPlayer.getItemList(); + boolean containsInfoOfActualPlayTrack=false; + for (int i = 0; i < album.length; i++) { + if(_cdPlayer.getInfoText().contains(expectedResult)) + containsInfoOfActualPlayTrack=true; + } + assertThat(containsInfoOfActualPlayTrack).describedAs(testName).isEqualTo(true); + } } static Stream getInfoTestData () { - CDPlayer cdPlayer1 = new CDPlayer(); + String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; + //some CDs + CD audioCD1=new CD("Audio",audioPlayList); + CDPlayer cdPlayer2 = new CDPlayer(); + cdPlayer2.tapOnCdFlap(); + cdPlayer2.setCD(audioCD1); + cdPlayer2.tapOnCdFlap(); + cdPlayer2.setInfoText("CDPlayer: Item->"+cdPlayer2.getActualPlayTrack()+" from Album->"+"AlbumName"+" running."); + return Stream.of( Arguments.of("[getInfoText() by infoText=null ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException(),""), - Arguments.of("[getInfoText() by infoText=null ] => ExceptionShouldReturnAMessage","Case2",cdPlayer1,new ReturnValueNullException(),"Method should not return a Null-Value.") + Arguments.of("[getInfoText() by infoText=null ] => ExceptionShouldReturnAMessage","Case2",cdPlayer1,new ReturnValueNullException(),"Method should not return a Null-Value."), + Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoOfActualPlayTrack","Case3",cdPlayer2,null,"") ); } From c5c7ea8c954b269d4335367ecc87594fca63efa9 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Wed, 16 Feb 2022 22:39:08 +0100 Subject: [PATCH 4/5] Method getInfoText of CDPlayer tested on MessageShouldContainInfoAboutAlbum. --- src/main/java/device/cdPlayer/CD.java | 9 +++++++++ src/main/java/device/cdPlayer/CDPlayer.java | 7 +++++-- src/test/java/device/cdPlayer/CDPlayerTest.java | 11 +++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/device/cdPlayer/CD.java b/src/main/java/device/cdPlayer/CD.java index e1f61b3..4934ac2 100644 --- a/src/main/java/device/cdPlayer/CD.java +++ b/src/main/java/device/cdPlayer/CD.java @@ -2,6 +2,7 @@ package device.cdPlayer; public class CD { private String format; + private String albumName; private String[] playList; public CD() {} @@ -30,4 +31,12 @@ public class CD { public void setPlayList(String[] playList) { this.playList = playList; } + + public String getAlbumName() { + return albumName; + } + + public void setAlbumName(String albumName) { + this.albumName = albumName; + } } diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 14ace41..07bb170 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -161,9 +161,12 @@ public class CDPlayer implements Device { return actualPlayTrack; } - public void setInfoText(String infoText) { - this.infoText=infoText; + public void setInfoText() { + this.infoText="CDPlayer: Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running."; } + public CD getCD() { + return this.loadedCD; + } } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index e6b26e9..8b9064e 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -296,6 +296,11 @@ class CDPlayerTest { } assertThat(containsInfoOfActualPlayTrack).describedAs(testName).isEqualTo(true); } + if(cases.equals("Case4")) { + String albumName= _cdPlayer.getCD().getAlbumName(); + assertThat(_cdPlayer.getInfoText().contains(albumName)).describedAs(testName).isEqualTo(true); + } + } static Stream getInfoTestData () { CDPlayer cdPlayer1 = new CDPlayer(); @@ -303,16 +308,18 @@ class CDPlayerTest { String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; //some CDs CD audioCD1=new CD("Audio",audioPlayList); + audioCD1.setAlbumName("Love Songs"); CDPlayer cdPlayer2 = new CDPlayer(); cdPlayer2.tapOnCdFlap(); cdPlayer2.setCD(audioCD1); cdPlayer2.tapOnCdFlap(); - cdPlayer2.setInfoText("CDPlayer: Item->"+cdPlayer2.getActualPlayTrack()+" from Album->"+"AlbumName"+" running."); + cdPlayer2.setInfoText(); return Stream.of( Arguments.of("[getInfoText() by infoText=null ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException(),""), Arguments.of("[getInfoText() by infoText=null ] => ExceptionShouldReturnAMessage","Case2",cdPlayer1,new ReturnValueNullException(),"Method should not return a Null-Value."), - Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoOfActualPlayTrack","Case3",cdPlayer2,null,"") + Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoOfActualPlayTrack","Case3",cdPlayer2,null,""), + Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoAboutAlbum","Case4",cdPlayer2,null,"") ); } From f51c6d783c63ce99f744944ffaff8ed3c83f4842 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Wed, 16 Feb 2022 22:44:52 +0100 Subject: [PATCH 5/5] Method getInfoText of CDPlayer tested on MessageShouldContainInfoAboutDevice. --- src/main/java/device/cdPlayer/CDPlayer.java | 2 +- src/test/java/device/cdPlayer/CDPlayerTest.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 07bb170..57101c8 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -162,7 +162,7 @@ public class CDPlayer implements Device { } public void setInfoText() { - this.infoText="CDPlayer: Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running."; + this.infoText=this.getClass().getSimpleName()+": Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running."; } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 8b9064e..e8d8f77 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -300,6 +300,9 @@ class CDPlayerTest { String albumName= _cdPlayer.getCD().getAlbumName(); assertThat(_cdPlayer.getInfoText().contains(albumName)).describedAs(testName).isEqualTo(true); } + if(cases.equals("Case5")) { + assertThat(_cdPlayer.getInfoText().contains("CDPlayer")).describedAs(testName).isEqualTo(true); + } } static Stream getInfoTestData () { @@ -319,7 +322,8 @@ class CDPlayerTest { Arguments.of("[getInfoText() by infoText=null ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException(),""), Arguments.of("[getInfoText() by infoText=null ] => ExceptionShouldReturnAMessage","Case2",cdPlayer1,new ReturnValueNullException(),"Method should not return a Null-Value."), Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoOfActualPlayTrack","Case3",cdPlayer2,null,""), - Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoAboutAlbum","Case4",cdPlayer2,null,"") + Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoAboutAlbum","Case4",cdPlayer2,null,""), + Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoAboutDevice","Case5",cdPlayer2,null,"") ); }