1

I'm just starting out with unit tests (junit). I saw a couple of videos from Roy Osherove, where he talked about good practices. Since I'm pretty new to this subject, I feel totally lost. Below is a short example of my code, which is then followed by how I tested so far and how I would create a fitting junit test. In essence I'm creating a card deck with 52 cards. I want to test if the deck contains all 52 cards, when I create a new "deck" object.

I may have some errors in my code, but it's more about the logic (see the questions in the end).

The production code


Imagine I want to create a common card deck, which consists of 4 suits and 13 ranks. Let's also assume that I created a "card" class and a "deck" class, that contains an instance variable, that's an array consisting of 52 cards.

The "card" class:

public class card {

private String[] suits = {"h", "s", "c", "d"};
private String[] ranks = {"2","3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"};

private String suit, rank;


public card (int rank, int suit) {
    this.suit = this.suits[suit];
    this.rank = this.ranks[rank];
}

public String getRank () {
    return rank;
}

public String getSuit () {
    return suit;
}
}

The "deck" class:

public class deck {

private card[] deckCards = new card[52];

public deck() {

    int i = 0;

    for(int suit = 0; suit<4; suit++) {
        for(int rank=0; rank<13; rank++) {
            this.deckCards[i] = new card(rank,suit);
            i = i+1;
        }
    }

}

public card[] getDeckCards() {
     return deckCards;
}
}

The Test


So, if I want to create a junit test, my test would look like the follow right?

public class Main {

public static void main(String[] args) {

    deck deck = new deck();
    int counter = 0;

    for (int i = 0; i<52; i++) {

        if (deck.deckCards[i] != null) counter++;


    }

    System.out.println("Cards in deck: " + counter);

    for (int i = 0; i<52; i++) {
        if (deck.deckCards[i] != null) System.out.println(deck.deckCards[i].getRank() + deck.deckCards[i].getSuit());
    }



}
}

As a result I would get something like this:

  • Cards in deck: 52
  • 2h,3h,4h,5h,6h,7h,8h,9h,Th,Jh,Qh,Kh,Ah, 2s,3s,4s,5s,6s,7s,8s,9s,Ts,Js,Qs,Ks,As, 2c,3c,4c,5c,6c,7c,8c,9c,Tc,Jc,Qc,Kc,Ac, 2d,3d,4d,5d,6d,7d,8d,9d,Td,Jd,Qd,Kd,Ad

Now I can look at all the cards and I would see that everything worked out like i wanted it. Obviously that's not how you test..

Now, if I wanted to create a Junit Test, I would do the following:

@Test
public void testDeck() {

    // create the expected value; which is 52 cards
    card[] expected = {new card(0,0),
                       new card(1,0),
                       new card(2,0),
                       ...          ,
                       new card(12,0),
                       new card(0,1),
                       ...          ,
                       new card(12,1),
                       new card(0,2),
                       ...          ,
                       new card(12,2),
                       new card(0,3),
                       ...          ,
                       new card(12,3)}



    // instantiate the deck
    deck carddeck = new deck();
    card[] actuals = carddeck.getCardDeck();

    //assert
    assertArrayEquals(expected, actuals);
}

Now, I have a few questions:

  1. Roy says that we shouldn't use ANY logic in a test method/case. Obviously I do use logic in to create my "expected", namely the constructor of the card class. But how can I use hard coded values? Easy when I do have string or a int as the expected, but what if I do have more complex structures, like this object?
  2. What if my card deck would have 1000 cards. Then it would be impossible to create an array with 1000 fields manually. But how could I test it otherwise?

Thanks so much for helping me :)

1
  • 1
    As Carl explained, use the class HashSet<Card>,
    – AlexWien
    Aug 4, 2015 at 18:42

3 Answers 3

2

The questions I would ask myself are:

  • If I change the algorithm (in this case the constructor of deck), what is likely to go wrong?
  • What are the key rules of the algorithm?
  • Which are the tests that fail immediately if something is wrong?

In your example I would test the following expectations:

  • The deck consists of 52 cards and no two cards are equal within the deck (add all cards to a Set and look if there are 52 cards within).
  • The border case cards are within the deck - maybe 2h, 2s, 2c, 2d, Ah, As, Ac, Ad.
  • If this would be a business case and some suits and/or ranks are more important than others I would add some extra tests for them.
1

the theories runner in junit can do this work better. theories runner will automatically generate all kinds of combinations of your test data.

  • use TestedOn annotation.

    @RunWith(Theories)
    public class DeckTest {
        @Theory
        public void testCard() {
                 @TestedOn(0, 1, 2, 3) int suit
                 @TestedOn(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) int rank) {                     
            Card card = new Card(rank, suit);
            Deck cardDeck = new Deck();
            Card[] cards = cardDeck.getDeckCards();
            assertThat(cards, hasItem(card)); // you should static import assertThat and hasItem
        }
    }
    
  • use DataPoints and FromDataPoints annotation

    @RunWith(Theories)
    public class DeckTest {
         @DataPoints("suits")
         public final static int[] SUITS = {0, 1, 2, 3};
         @DataPoints("rnaks")
         public final static int[] RANKS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    
         @Theory
         public void testCard(
                         @FromDataPoints("suits") int suit,
                         @FromDataPoints("ranks") int rank) {
             Card card = new Card(rank, suit);
             Deck cardDeck = new Deck();
             Card[] cards = cardDeck.getDeckCards();
             assertThat(cards, hasItem(card));
         }
    
    }
    
0

You know that you want 52 different cards from this method, and you know that there are only 52 possible cards. So you could create an equals() method which compares the suit and rank - test-drive that method, of course - and then your test for getCardDeck could put all of the cards from the array into a set. If your set contains 52 cards, your method is working, because there are only 52 possibilities, and a set will only hold one copy of any card, so if your 52-element array held any duplicates, the set would be smaller.

4
  • Thanks for your answer, but that doesn't really help me. Please consider that I'm pretty new to programming and testing. It also seems to miss my questions in the end ;-)
    – cobby
    Aug 4, 2015 at 18:51
  • I'm sorry, but I really believe this does answer your questions. How do you test that 52 distinct cards have been returned? Create a set, put all the cards from your deck into it, and if the set has 52 elements, your code is working; if it doesn't, your code isn't working. It's that simple. Aug 4, 2015 at 18:54
  • Well, the question is if my deck constructor does create a 52 card deck in the first place. I know it's very simple code, that probably doesn't really get tested at all. I used it as a springboard to find an answer to a much more abstract problem. How do I test big structures, without using logic (meaning any ifs, loops, basically any production code)? Obviously I need to hard code my end result. That's what Roy Osherove is suggesting.
    – cobby
    Aug 4, 2015 at 19:13
  • I think it is a mistake to be dogmatic about avoiding logic in your test. There is nothing "obvious" about needing to hardcode your end result. When you have a clear, simple way to verify that code behaves you want - even if it contains logic - use it. Aug 4, 2015 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.