6

I want a test class to test this class but i dont know how to write it and i tried to see online but i still couldnt figure it out.I wrote the code on BlueJ, i'm trying to create the set game.

import java.util.*;

public class Deck
{
    ArrayList<Card> deck;
    public Deck ()
    {
         deck = new ArrayList<Card>();
    }

     public Deck (int capacity)
    {
        deck = new ArrayList<Card>(capacity);
    }

    public int getNumCards ()
    {
        return deck.size();
    }

    public boolean isEmpty () 
    {
        return deck.isEmpty();
    }

    public void add (Card card) 
    {
        deck.add(0,card);
    }

    public Card takeTop() 
    {
        return deck.remove(0);
    }

    public void shuffle ()
    {
        Collections.shuffle(deck);
    }

    public void sort ()
    {
        Collections.sort(deck);
    }

    public String toString ()
    { 
         return (deck.toString()+ "\n");
    }
}
royhowie
  • 10,605
  • 14
  • 45
  • 66
user2022871
  • 61
  • 1
  • 1
  • 4

5 Answers5

5

First you need to decide on the what test cases you need to write for your class , You can use a library like Junit to create test cases once you have the test case list handy.

Here is an example of a few Junit methods

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class MyClassTest {

  MyClass tester;

  @BeforeClass
  public static void testSetup() {
    tester = new MyClass();
  }

  @AfterClass
  public static void testCleanup() {
    // Do your cleanup here like close URL connection , releasing resources etc
  }

  @Test(expected = IllegalArgumentException.class)
  public void testExceptionIsThrown() {        
    tester.divide(1000, 0);
  }

  @Test
  public void testMultiply() {
    assertEquals("Result", 50, tester.multiply(10, 5));
  }
} 
Avinash Singh
  • 2,564
  • 1
  • 14
  • 20
  • Remove that redundand initialization by pulling it into @Before or something like that. Naming the object which is being tested 'cut' is a nice convention, imo. – atamanroman Mar 08 '13 at 19:55
2

Use a testing framework like Junit, see the sample below,

    public class ThingTester extends TestCase
{
    public ThingTester (String name) 
    {
        super (name);
    }

    public static void main(String[] args) 
    {
        TestRunner.runAndWait(new TestSuite(ThingTester.class));
    }

    public void testGetName() throws Exception 
    {
        String fileSpec = new String("c:xxxyyyzzz.txt");
        assertEquals("zzz.txt", getName(fileSpec));
    }
}
Orn Kristjansson
  • 3,417
  • 4
  • 23
  • 37
1

You need to create the main method that would test the functionality of your class.

public static void main(String args[])
{
    //To do
}

In your main method you need to for example construct a Card object (assuming you have the Card class).

Card card = new Card();

Then you also need to construct a Deck object, which you would use to call methods of the Deck class in order to for example add cards to the Deck

Deck deck = new Deck();

Use the deck object to call the add method to add the card to the Deck

deck.add(card);

So now your main method should look something like this:

public static void main(String args[])
{
   Card card = new Card();
   Deck deck = new Deck();
   deck.add(card);
}

Also in your Deck class, I'd recommend using List<Card> deck = new ArrayList<Card>(); rather than ArrayList<Card> deck = new ArrayList<Card>();.

Hope this gives you a starting point.

Kakalokia
  • 3,013
  • 2
  • 19
  • 40
  • Testing frameworks are there for a reason. Your class is a *driver*, testing implies to assert a result. – atamanroman Mar 08 '13 at 19:57
  • I didn't feel exposing the OP to it was advantageous to him if he still doesn't know how to create a test class for his program. From the OP description I thought he needed a driver class to test his program. – Kakalokia Mar 08 '13 at 19:59
0

I think I didn't understand what you want, but I will give my suggestion here any way.

Where is Card class?

Add this method in your Deck class, compile your code and execute.

public static void main(String[] args) {
    Deck deck = new Deck();
    // Call your methods here and do what do you want...
}
0

If you are in Blue Jay you can simply right click on the class and at the bottom of the pop - up, there will be an option for "Create Test Class". Using this will simplify the process. Below I have provided an example of what Blue Jay creates.

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The test class TestOfClass2Test.
 *
 * @author  (your name)
 * @version (a version number or a date)
 */
public class TestOfClass2Test
{
    /**
     * Default constructor for test class TestOfClass2Test
     */
    public TestOfClass2Test()
    {
    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    @Before
    public void setUp()
    {
    }

    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    @After
    public void tearDown()
    {
    }
}