Henrik
Henrik

Reputation: 1807

Testing a method producing a random result

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?

Upvotes: 2

Views: 3288

Answers (4)

Mike Samuel
Mike Samuel

Reputation: 120516

Instead of creating a RNG in your method

public void playRandomSong() {
  Random random = new Random();
  ...
}

you should pass the source of randomness in (this is called dependency injection)

public void playRandomSong(Random random) {
  ...
}

and then you can generate a Random instance with a known seed in your unit test to get repeatable but typical results.

public void testPlayRandomSong() {
  Random random = new Random(0xd5021e90339050ab);

  // Test that 1000 runs plays each song roughly the right number of times.
  ...
}

Upvotes: 4

Frank
Frank

Reputation: 15641

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 Set around the Random, 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 arrayList

With Option 1 you will have to change the signature of your method.

With Option 2 you will also play every song once.

Upvotes: 4

BrownFurSeal
BrownFurSeal

Reputation: 515

I see another problem in your code: if you want to play songs in random order you are doing that in wrong way. This algorithm should not repeat song until all songs in list are played. To achieve that there is an algorithm called Knuth shuffling. You can take it from Collections class: java.util.Collections#shuffle.

Upvotes: 2

Cedric Beust
Cedric Beust

Reputation: 15608

Here is an article on randomness testing that you might find useful:

http://beust.com/weblog/2012/02/20/various-ways-to-get-randomness-wrong/

Upvotes: 0

Related Questions