JUnit测试类失败测试

时间:2013-10-25 13:53:12

标签: java unit-testing junit

在我的GradeBook类程序中,我应该在GradeBook类中添加一个toString方法,该方法返回一个字符串,每个分数的分数用空格分隔。

我已经完成了,当我创建JUnit测试时,我应该使用toString方法来比较得分数组中的内容与得分数组中预期的内容以及编译和运行时的内容JUnit测试addScore测试应该是真的。

我们要使用的方法是assertTrue()方法。 一个前。我们老师给我们的是它应该是以下形式:

assertTrue(g1.toString.equals("50.0 75.0"); the 50 and 75 are scores taken from the objects of GradeBook made in the JUnit Test Class in the setUp() method but mine will be Totally different.

现在,当我编译并运行prorgram时,JUnit Test表示存在错误     assertTrue()代码,但我不明白为什么。 我也很肯定我的toString是正确的形式,但如果有人可以阐明我的程序,我会很感激

GradeBook代码是:

import java.util.ArrayList;
import java.util.Arrays;

public class GradeBook
{
private double[] scores;
private int scoresSize;

/**
  Constructs a gradebook with no scores and a given capacity.
  @capacity the maximum number of scores in this gradebook
 */
public GradeBook(int capacity)
{
    scores = new double[capacity];
    scoresSize = 0;
}

/**
  Adds a score to this gradebook.
  @param score the score to add
  @return true if the score was added, false if the gradebook is full
 */
public boolean addScore(double score)
{
    if (scoresSize < scores.length)
    {
        scores[scoresSize] = score;
        scoresSize++;
        return true;
    }
    else
        return false;      
}

/**
  Computes the sum of the scores in this gradebook.
  @return the sum of the scores
 */
public double sum()
{
    double total = 0;
    for (int i = 0; i < scoresSize; i++)
    {
        total = total + scores[i];
    }
    return total;
}

/**
  Gets the minimum score in this gradebook.
  @return the minimum score, or 0 if there are no scores.
 */
public double minimum()
{
    if (scoresSize == 0) return 0;
    double smallest = scores[0];
    for (int i = 1; i < scoresSize; i++)
    {
        if (scores[i] < smallest)
        {
            smallest = scores[i];
        }
    }
    return smallest;
}

/**
  Gets the final score for this gradebook.
  @return the sum of the scores, with the lowest score dropped if 
  there are at least two scores, or 0 if there are no scores.
 */
public double finalScore() 
{
    if (scoresSize == 0)
        return 0;
    else if (scoresSize == 1)
        return scores[0];
    else
        return sum() - minimum();
}

public int getScoresSize() {
    return scoresSize;
}

public String toString(String scoreList){


    for(int i = 0; i < scores.length; i++){

        scoreList += scores[i] + "";

    }
    return scoreList;
}

}

这是JUnit测试类:

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class GradeBookTest {

private GradeBook g1;
private GradeBook g2;

@Before
public void setUp() throws Exception {

    g1 = new GradeBook(5);
    g2 = new GradeBook(5);

    g1.addScore(45);
    g1.addScore(68);
    g1.addScore(35);
    g1.addScore(22);

    g2.addScore(99);
    g2.addScore(10);
    g2.addScore(77);
    g2.addScore(43);
}

@After
public void tearDown() throws Exception {

    g1 = null;
    g2 = null;

}

@Test
public void testAddScore() {

    assertTrue(g1.toString().equals("45.0 68.0 35.0 22.0"));
    assertTrue(g2.toString().equals("99.0 10.0 77.0 43.0"));

    assertEquals(45,g1.getScoresSize(),.001);
    assertEquals(68,g1.getScoresSize(),.001);
    assertEquals(35,g1.getScoresSize(),.001);
    assertEquals(22,g1.getScoresSize(),.001);

    assertEquals(99,g2.getScoresSize(),.001);
    assertEquals(10,g2.getScoresSize(),.001);
    assertEquals(77,g2.getScoresSize(),.001);
    assertEquals(43,g2.getScoresSize(),.001);
}

2 个答案:

答案 0 :(得分:1)

你的toString方法有一个签名:

public String toString(String scoreList)

它需要参数,你可以调用它:

g1.toString()

将您的toString方法更改为:

public String toString(){

    String scoreList = "";

    for(int i = 0; i < scores.length; i++){

        scoreList += scores[i] + " "; //remember to add a space here!

    }
    return scoreList;
}

答案 1 :(得分:0)

刚发现这个......常见的错误......

g1.toString.equals(...)

应该是

g1.toString().equals(....)

注意toString之后的附加括号 - toString是一个方法,需要调用。