I have a method that is called shuffle, like the one I have shown below, that shuffles an ArrayList, of integers. I was wondering, do I have to test this method?
public void shuffle(){
Collections.shuffle(numbers);
}
I am using this method to shuffle a deck of 52 cards, and in this case, it is kind of harder to see(by testing if the cards are shuffled or not, because you might have two cards in a row with the same number or the same suit, which is still considered shuffled.
There is still a point in testing trivial routines. You can test that
Collections.shuffleif that’s a requirement of the routinewhich will let you know that the routine still upholds its contract should you desire to
refactorlater on.Do you need to test it? No. Is it pragmatic to test? Perhaps not. But every bit of guarantee helps to verify the correctness of your program. Ultimately, it’s for you to decide.
EDIT
It’s good practice to design for testing. Essentially it’s easier to test a routine which has no side effects, i.e.,
Now since
Collections.shufflemodifies an existing list a return value would seem ambiguous. Let’s consider the second best option for testabilityWhether such design is appropriate or not in your case I leave for you to judge. This was just a general fyi 🙂