I want to test this method:ArrayList<File> songs;
public void playRandomSong()
{
Random random = new Random();
int iNextSong = random.nextInt(songs.size());
File songToPlay = songs.get(iNextSong);
if (mediaPlayer != null && mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer = new MediaPlayerImpl(songToPlay, new WhenDone());
mediaPlayer.play();
currentSong = songToPlay;
}
I’m thinking in this way: Run the method multiple times and see if it returns one of the elements more than once. But how would I write that in code?
Random does not guarantee that it will not return the same value twice…
So you can not test
"see if it returns one of the elements more than once"If you need that you will have to implement a
Setaround theRandom, but be aware of the Birthday paradox…I think you have 2 options:
1 : You may try to seed your
Random, so you can predict the sequence…2 : Remove the Random and make use of the
[Collections.shuffle][1]to shuffle you arrayListWith Option 1 you will have to change the signature of your method.
With Option 2 you will also play every song once.