@ -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,56 @@ class CDPlayerTest {
assertThat ( volumeIstInValueRange ) . isEqualTo ( true ) ;
}
@ParameterizedTest
@MethodSource ( "getInfoTestData" )
void getInfoTest ( String testName , String cases , CDPlayer _cdPlayer , Exception exception , String expectedResult ) {
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 ) ;
}
if ( cases . equals ( "Case4" ) ) {
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 < Arguments > 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 ) ;
audioCD1 . setAlbumName ( "Love Songs" ) ;
CDPlayer cdPlayer2 = new CDPlayer ( ) ;
cdPlayer2 . tapOnCdFlap ( ) ;
cdPlayer2 . setCD ( audioCD1 ) ;
cdPlayer2 . tapOnCdFlap ( ) ;
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 ] => MessageShouldContainInfoAboutAlbum" , "Case4" , cdPlayer2 , null , "" ) ,
Arguments . of ( "[getInfoText() by infoText=Message ] => MessageShouldContainInfoAboutDevice" , "Case5" , cdPlayer2 , null , "" )
) ;
}
}