1

Survey.java クラスと SurveyTest.java JUnit テストを作成しました。しかし、Survey クラスでリストをテストする方法がわかりません。JUnit テストでそれらをテストするにはどうすればよいですか?

調査.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");
}

}
4

3 に答える 3

5

そのクラスには実際のロジックがないため、このクラスをテストする必要はありません。ある種のロジックを含むクラスのみをテストする必要があります。

于 2012-05-26T22:41:12.953 に答える
0

Survey.java は、値を保護するためのモデル/データ構造です。理想的には、Survey クラスを使用しているクラスをテストする必要があります。例のために。CreateSurvey は調査を作成するクラスである可能性があるため、create メソッドを単体テストして、調査オブジェクトの値が適切に設定されていることを確認できます。

于 2012-05-27T03:26:46.240 に答える
0

リストがnullかどうかをテストできます

于 2013-11-12T09:17:59.063 に答える