Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit测试类失败测试_Java_Unit Testing_Junit - Fatal编程技术网

Java JUnit测试类失败测试

Java JUnit测试类失败测试,java,unit-testing,junit,Java,Unit Testing,Junit,在成绩册类的程序中,我应该向成绩册类添加一个toString方法,该方法返回一个字符串,每个分数以空格分隔 我已经这样做了,当我创建JUnit测试时,我应该使用toString方法来比较scores数组中的内容与scores数组中预期的内容,当我编译并运行JUnit测试时,addScore测试应该是真的 我们要使用的方法是assertTrue()方法。 我们老师给我们的一个例子是它应该是这样的: assertTrue(g1.toString.equals("50.0 75.0"); the 50

在成绩册类的程序中,我应该向成绩册类添加一个toString方法,该方法返回一个字符串,每个分数以空格分隔

我已经这样做了,当我创建JUnit测试时,我应该使用toString方法来比较scores数组中的内容与scores数组中预期的内容,当我编译并运行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测试表明 assertTrue()代码,但我不明白为什么。 我也很肯定我的toString的形式是正确的,但如果有人能对我的程序有所了解,我将不胜感激

成绩册代码为:

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;
}

}

您的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;
}
公共字符串toString(){
字符串scoreList=“”;
for(int i=0;i
刚刚发现了这个。。。常见的错误

g1.toString.equals(...)
应该是

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

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

请添加编译错误。如果我们知道你的问题是什么,那么诊断你的问题就会容易得多。你的成绩册::toString需要一个分数表字符串作为参数。也许这就是问题所在?@atk错误显示:GradeBookTest.testAddScore(GradeBookTest.java:41)的java.lang.AssertionError,然后当我单击它时,它会突出显示assertTrue代码。我更改了字符串方法,但出现了相同的错误。GradeBookTest.testAddScore(GradeBookTest.java:41)上的错误为:java.lang.AssertionError。现在这是不同的错误。它表示代码正常,但测试函数在第41行返回不同的值。代码中哪一行是第41行?实际上,您应该从“45.0 68.0 35.0 22.0”(应该是:“45 68 35 22”)中删除这些零(存在一个空格问题)-请参阅更新的replyIt它是资产true(g1.toString().equals(“45.0 68.0 35.0 22.0”));即使我删除了零,错误仍然显示这是教授教我们使用的形式,但现在我不知道为什么它不起作用是的,我在for循环中的引号中添加了一个空格是的,在从before@user2807804:在这种情况下,请使用您需要的正确语法更新您的问题re using.why the down vote?有人不认为您需要在Java中使用括号来调用toString()方法吗?
g1.toString().equals(....)