mardi 30 août 2016

Java Constructor being called multiple times

I'm creating a PokerHand class and using JUnit tests to test it, and for some reason my constructor is called 8 times when I call it in one test method, when I create one PokerHand object.

PokerHand constructor (added a print statement to see how many times it was being called, prints 8 separate times):

//Constructor
public PokerHand (Card cardOne, Card cardTwo, Card cardThree, Card cardFour, Card cardFive) {
    System.out.println("creating hand...");
    //Initialize value array
    value = new int[6];
    //Initialize cards list, add cards, and check for duplicates
    cards = new ArrayList<Card>();

    cards.add(cardOne);
    if (cards.contains(cardTwo)) {
        throw new DuplicateCardException();
    } else cards.add(cardTwo);
    if (cards.contains(cardThree)) {
        throw new DuplicateCardException();
    } else cards.add(cardThree);
    if (cards.contains(cardFour)) {
        throw new DuplicateCardException();
    } else cards.add(cardFour);
    if (cards.contains(cardFive)) {
        throw new DuplicateCardException();
    } else cards.add(cardFive);

    determineValueOfHand();
}

Test case:

    @Test
  public void testFlush() {
      PokerHand a = new PokerHand(D10, DJ, DQ, DK, DA);
  }

I've been staring at a screen for some time and I'm new to JUnit tests and eclipse, so I'm sure I'm just missing a small detail. Any help is greatly appreciated

Aucun commentaire:

Enregistrer un commentaire