8

我想要一个测试类来测试这个类,但我不知道如何编写它,我试图在网上看到但我仍然无法弄清楚。我在 BlueJ 上编写了代码,我正在尝试创建设置游戏。

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");
    }
}
4

5 回答 5

7

首先你需要决定你需要为你的类编写哪些测试用例,一旦你手头有测试用例列表,你就可以使用像 Junit 这样的库来创建测试用例。

下面是几个 Junit 方法的示例

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));
  }
} 
于 2013-03-08T19:50:34.843 回答
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));
    }
}
于 2013-03-08T19:54:01.083 回答
1

您需要创建将测试您的类的功能的主要方法。

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

例如,在您的主要方法中,您需要构造一个 Card 对象(假设您有 Card 类)。

Card card = new Card();

然后,您还需要构造一个 Deck 对象,您可以使用该对象调用 Deck 类的方法,例如将卡片添加到 Deck

Deck deck = new Deck();

使用deck对象调用add方法将卡片添加到Deck

deck.add(card);

所以现在你的主要方法应该是这样的:

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

同样在您的 Deck 课程中,我建议使用List<Card> deck = new ArrayList<Card>(); 而不是ArrayList<Card> deck = new ArrayList<Card>();.

希望这能给你一个起点。

于 2013-03-08T19:48:49.837 回答
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...
}
于 2013-03-08T19:54:03.797 回答
0

如果您在 Blue Jay 中,您只需右键单击该类,然后在弹出窗口的底部,将有一个“创建测试类”选项。使用它可以简化流程。下面我提供了一个 Blue Jay 创建的示例。

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()
    {
    }
}
于 2016-11-11T03:24:34.343 回答