2013-03-08 174 views
5

我想一個測試類測試這個類,但我不知道怎麼寫呢,我試着在網上看到,但我仍想不出看着辦吧out.I寫了一篇關於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"); 
    } 
} 

回答

0

您需要創建測試類的功能的主要方法。

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

在你的主要方法中,你需要例如構造一個Card對象(假設你有Card類)。

Card card = new Card(); 

那麼你還需要構建一個甲板對象,你會用它來調用甲板類的方法,以便例如添加卡到甲板

Deck deck = new Deck(); 

使用甲板對象調用add方法將卡添加到甲板

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>();

希望這給你一個起點。

+0

測試框架是有原因的。你的課是一個*驅動*,測試意味着要聲明一個結果。 – atamanroman 2013-03-08 19:57:50

+0

我覺得如果他不知道如何爲自己的程序創建一個測試課,那麼他就不會覺得OP對他有好處。從OP描述中我認爲他需要一個駕駛員級來測試他的程序。 – 2013-03-08 19:59:10

4

首先,您需要確定需要爲您的課程編寫哪些測試用例, 您可以使用像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)); 
    } 
} 
+0

通過將它拖入@Before或類似的東西來刪除冗餘和初始化。命名正在測試'剪切'的對象是一個很好的約定,imo。 – atamanroman 2013-03-08 19:55:15

1

使用一個測試框架Junit的類似的例子,請參見下面的示例,

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)); 
    } 
} 
0

我想我不明白你想要什麼,但我會以任何方式提出我的建議。

哪裏是卡類?

在你的卡類添加這個方法,編譯代碼並執行。

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

這也是*驅動程序*。你必須斷言一個結果。 – atamanroman 2013-03-08 20:00:19

+1

你是激勵我們去嘗試幫助某人的原因,因爲你沒有任何理由就冷靜下來......我知道什麼是測試課,但是問題的本身可能不是,所以讓他選擇他想要的並且投票得更好回答... – 2013-03-08 20:27:40

0

如果你在藍鳥,你可以在類,並在彈出的底部,只需右鍵單擊 - 時,將會有「創建測試類」的選項。使用這將簡化過程。下面我提供了一個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() 
    { 
    } 
} 
相關問題