Home How do I create a test class that can verify a set of playing cards?
Reply: 1

How do I create a test class that can verify a set of playing cards?

Gabby Kim
1#
Gabby Kim Published in 2017-12-06 19:51:51Z

I'm trying to code a card game in Java through the usage of classes. I have to create a test class that verifies the results of the methods called as well as to printing out a banner with each test that states whether the test failed or passed as well as a final banner to see if all the test passed or failed.

PlayingCard Class (This class needs to be made into a tester)

import java.util.ArrayList;
import java.util.Random;

public class PlayingCards
{
    private ArrayList<Card> m_Cards;
    private Random gen;

    /**
     No-arg constructor initializes m_Cards.

     Initialize the ArrayList head and the random number generator.
     */
    public PlayingCards()
    {
        m_Cards = new ArrayList<Card> ();
        gen = new Random ();
    }

    /**
     Get the number of cards.    

     @return number of cards
     */
    public int size()
    {
        return m_Cards.size();
    }

    /**
     Add a card.
     */
    public void addCard(Card newCard)
    {
        m_Cards.add(newCard);
    }

    /**
     Remove a card.

     @return null if there are no cards to remove or the instance field is null.
             Otherwise, returns a reference to a Card.
     */
    public Card removeCard()
    {
        if (m_Cards.size() == 0)
            return null;

        else
            return m_Cards.remove(0);
    }

    /**
     Shuffle the cards.

     This method shuffles the cards.
     */
    public void shuffle()
    {
        if ((m_Cards == null) || (m_Cards.size() < 2))
            return;

        for (int ii = 0; ii < m_Cards.size(); ++ii)
        {
            Card a = m_Cards.get(ii);
            int swapIndex = gen.nextInt(m_Cards.size());

            Card b = m_Cards.get(swapIndex);

            // swap the positions of a and b
            m_Cards.set(ii, b);
            m_Cards.set(swapIndex, a);
        }
    }

    /**
     Implement the toString method to show the contents of a
     PlayingCards object.

     This method relies on Card.toString() method.
     */
    @Override
    public String toString()
    {
        String outPut = "";

        for (int i = 0; i < m_Cards.size(); i++)
        {
            outPut += m_Cards.get(i).toString();
            outPut += "\n";
        }

        return outPut;
    }
}
Rohan
2#
Rohan Reply to 2017-12-06 20:03:24Z

If you do not want to use JUnit:

Write a new class that creates an instance of PlayingCards and then calls methods on that class. Use assert to verify that what you expect to happen happens. For example:

public class PlayingCardsTest {
  private PlayingCards playingCards;

  PlayingCardsTest() {
    playingCards = new PlayingCards();
  }

  public void testAddCard() {
    playingCards.addCard(new Card());

    assert playingCards.size() == 1;
  }
}

You can then create a main that will create a new PlayingCardsTest and call each test method.

You can also use JUnit to make your testing much easier. With JUnit, your test might look like:

public class PlayingCardsTest {
  private PlayingCards playingCards = new PlayingCards();

  @Test
  public void testAddCard() {
    playingCards.addCard(new Card());

    assertEquals(1, playingCards.size());
  }
}

Then you would run your JUnit tests with the JUnitRunner.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.256394 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO