2012-05-26 20 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

可以测试列表是否为空或不是