2012-05-26 28 views
1

我創建了一個Survey.java類和一個SurveyTest.java JUnit測試。但我不確定如何測試Survey類中的列表。我如何在JUnit測試中測試它們?如何在JUnit測試中測試列表?

Survey.java

package com.jhaksurvey.model; 

import java.util.List; 

public class Survey { 

private long id; 
private String title; 
private boolean active = true; 
private List<Question> questions; 

public Survey() { 

} 

public Survey(long id, String title) { 
this.id=id; 
this.title=title; 
} 

public long getId() { 
return id; 
} 

public void setId(long id) { 
this.id = id; 
} 

public String getTitle() { 
return title; 
} 

public void setTitle(String title) { 
this.title = title; 
} 

public boolean isActive() { 
return active; 
} 

public void setActive(boolean active) { 
this.active = active; 
} 

public List<Question> getQuestions() { 
return questions; 
} 

public void setQuestions(List<Question> questions) { 
this.questions = questions; 
} 

} 

SurveyTest.java

package com.survey.model.test; 

import junit.framework.Assert; 
import junit.framework.TestCase; 

import com.survey.model.Survey; 


public class SurveyTest extends TestCase { 

private Survey survey; 

protected void setUp() throws Exception { 
    super.setUp(); 
    survey = new Survey(); 
} 

public void testSurvey() { 
    survey.toString(); 
} 

public void testSurveyLongString() { 
    fail("Not yet implemented"); 
} 

public void testGetId() { 
    long expected = (long) Math.random(); 
    survey.setId(expected); 
    long actual = survey.getId(); 
    Assert.assertEquals(expected, actual); 
} 

public void testGetTitle() { 
    String expected = "surveytitle"; 
    survey.setTitle(expected); 
    String actual = survey.getTitle(); 
    Assert.assertEquals(expected, actual); 
} 

public void testIsActive() { 
    Boolean expected = true; 
    survey.setActive(expected); 
    Boolean actual = survey.isActive(); 
    Assert.assertEquals(expected, actual); 
} 

public void testGetQuestions() { 
    fail("Not yet implemented"); 
} 

} 
+1

你基本上測試,如果getter和setter方法正在努力 - 在你的情況,這些都是如此簡單,有這沒有附加值。您正在測試Java和JVM(變量賦值/讀取)的基礎知識,這是一個膨脹。 如果您向班級添加了一些相關業務邏輯,則不適合對其進行測試。測試製定者和獲取者僅適用於人爲地增加代碼覆蓋率指標。 –

回答

5

有沒有在這個類沒有真正的邏輯,所以你不需要測試這個類。你只應該測試包含某種邏輯的類。

0

Survey.java是一個模型/數據結構來引導值。理想情況下,你應該測試使用Survey類的類。例如。 CreateSurvey可能是創建調查的類,因此您可以對創建方法進行單元測試,以確保它適當地設置調查對象的值。

0

可以測試列表是否爲空或不是