2016-01-07 147 views
-2

這是我第一次不得不寫一個J單元測試,我一直在堅持如何開始。這個課程代表一個奧賽羅板上的單個單元格,它有一個網格和令牌值。JUnit測試一個班級

我想測試「黑色」和「白色」和不同位置的構造函數,我也想測試所有的setters和getters。

任何幫助將不勝感激。

public class BoardCell 
{ 
    /** 
    * The Item at this BoardCell. 
    */ 
    private Item token; 

    /** 
    * The CellLocation of this BoardCell. 
    */ 
    private BoardLocation location; 

    /** 
    * Constructor. 
    * @param row the row number. 
    * @param col the column number. 
    * @param token the Item value. 
    */ 
    public BoardCell(int row, int col, Item token) 
    { 
     this.token = token; 
     location = new BoardLocation(row, col); 
    } 

    /** 
    * Sets the Item value. 
    * @param token the Item value. 
    */ 
    public void setItem(Item token) 
    { 
     this.token = token; 
    } 

    /** 
    * Set the value of the Item in this BoardCell. 
    * @param val the value of the Item. 
    */ 
    public void setValue(String val) 
    { 
     this.token.setValue(val); 
    } 

    /** 
    * Gets the Item value. 
    * @return the Item at this BoardCell. 
    */ 
    public Item getItem() 
    { 
     return token; 
    } 

    /** 
    * Get the BoardLocation for this BoardCell. 
    * @return the BoardLocation for this BoardCell. 
    */ 
    public BoardLocation getLocation() 
    { 
     return location; 
    } 
} 
+0

你在使用IntelliJ嗎?如果是這樣,請嘗試突出顯示BoardCell並按住Shift-Ctrl-T。它會爲你建立一個測試。 – rajah9

回答

2

這將是最好的開始與在junit site和一些教程的信息。另外,最好的做法是首先編寫測試,然後編寫測試的功能。這就是說,這裏有一個大綱:

1)拿出你想要的測試列表。關注每個功能以及它可以使用的不同方式(以及它可能出錯的方式)。

2)編寫預期的行爲比較實際的性能測試,將是這樣的:

@Before 
    public void setUp() { 
     // In this function you'll want to create instances of the class that you will then test 
    } 

    @Test 
    public void testSomething() { 
    assertEquals(BoardCell.getItem(), whatever you think it should equal); //or whatever you're testing 
    } 

    @Test 
    public void etcetera() . . . 
+1

爲什麼不使用assertEquals?當對象不相等時它會提供更好的輸出(假設你重寫'toString'方法) –

4

你需要創建一個新的類,將保留您的所有不同的測試開始,你可以調用它,說,BoardCellTestBoardCellTestCase

在本課程中,您將需要添加不同的測試用例,這些測試用例是用@Test註解的public void方法。

然後,每個方法都應該聲明(使用Assert類中的方法)需要測試的內容,方法是創建正確的BoardCell對象並獲取其值或設置新值。

0

您是在尋找代碼覆蓋範圍,或者只是爲了查看某個方法是否按照您的預期方式工作?如果您只是在尋找覆蓋範圍,那麼只需創建一個類並使用@Test對方法進行註釋,大多數覆蓋工具就會選擇JUnit/Surefire報告並對它們進行適當的解釋。

如果你想測試一些類似於getter或setter的東西,那麼你可以使用一個簡單的斷言測試。

@Test 
public void testGetItem() throws Exception { 
    Item item = new BlackItem(); 
    BoardCell cell = new BoardCell(128, 256, item); 
    assertNotNull("item should not be null", cell.getItem()); 
    assertEquals("items should be considered equal", item, cell.getItem()); 
} 

如果替換類toString那麼如果斷言失敗,JUnit會輕易地打印出的toString的結果,這是真的爲assertEqualsassertArrayEquals(我相信)和assertThat。我沒有例外的精確副本手,但它會沿着此線的東西:

org.junit.AssertionError: Expected Item{name="Black Item"} but was Item{name="White Item"} 
Expected: Item{name="Black Item"} 
Actual: Item{name="White Item"} 

東西,可能派上用場的測試BoardLocationItem對象的平等。閱讀更多關於斷言的好地方是JUnit網站,正如其他海報和他們的GitHub wiki所提到的那樣,我個人更喜歡這個網站。