I want to ensure that after the shuffleDeck() method returns the deck of cards in a random order. Currently I'm just testing by checking the value of the isShuffled field. But I can't think of a way to test whether the contract of deck of cards in random order is fufilled [ August 05, 2006: Message edited by: Garrett Rowe ]
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Using the Collections.shuffle() method you should rely on that method being properly tested by its implementor; thus you really shouldn't need to test for randomness in your unit test. What you might want to make sure is that Collections.shuffle() is actually called. Then again, that's not all that easy to do, since static methods generally can't be mocked...
I'd probably be happy with testing that the flag is set correctly and then assuming that this is too simple to break.
I would add a bit more to the test to make sure it doesn't pass if the Collections.shuffle(deck) line is removed.
For example, shuffle the deck check the cards aren't in the original order. While it is possible for the shuffled deck to be in the same order, you can reduce this possibility even further by shuffling multiple times.
But I can't think of a way to test whether the contract of deck of cards in random order is fufilled
Just want to clarify that I am talking about making sure the method is called, not making sure Sun met the contract of the shuffle method. In other words, that your method meets its contract assuming that Sun's method does.
If this was more complicated, you could create a wrapper for the Collections method. But here I think that is more trouble than it is worth.
Joined: Jul 26, 2004
Originally posted by Jeanne Boyarsky: For example, shuffle the deck check the cards aren't in the original order. While it is possible for the shuffled deck to be in the same order, you can reduce this possibility even further by shuffling multiple times.
Well, if you check that the cards aren't in the original order you verify that you did something to the collection, which might be considered better than nothing, but you're in no way checking that the correct method is called. IMO the benefit of that is very limited (as is - admittedly - just checking that the flag is set correctly).
Garrett, Assuming your equals() method on Deck is implemented to check whether the contents of the deck are equal (as opposed to using object.equals()), this is fine.
There is a slight chance that a shuffle may result in the exact same order, but I think it is too small to be worth worrying about.
The last two lines in your test are redundant though. If you want to use the technique of shuffling twice to reduce the probablity of a shuffle being in the original order, you need to use some logic:
Joined: Jan 17, 2006
I was using the second test to test that the order of the deck after the shuffle() method was called was really random, as oppposed to to some predefined swapping of cards.
i.e. starting with two decks in the same order (a.equals(b)) and invoking the shuffle() method on both decks that they will (likely) now have a different ordering (!a.equals(b)). [ August 09, 2006: Message edited by: Garrett Rowe ]