junit.framework.AssertionFailedError - 给定的堆栈跟踪不完整

junit.framework.AssertionFailedError - Incomplete Stack Trace Given

完全披露:这是一项作业,已被标记,但我想了解为什么会出现此错误。

我在理解为什么 junit.framework.AssertionFailedError 被抛出时遇到了一些问题。通常,当发生错误时,我至少可以查看堆栈跟踪并了解发生了什么。在这种情况下,输出控制台显示:

Testcase: testIsCorrectMCQ(mr_3.myTester):  FAILED
null
junit.framework.AssertionFailedError
    at mr_3.MyTester.testIsCorrectMCQ(Assign03Tester.java:207)
testIsCorrectMCQ(mr_3.MyTester):    FAILED

在 NetBeans 的测试结果选项卡中,复制堆栈跟踪得到:

junit.framework.AssertionFailedError 
    at mr_3.myTester.testIsCorrectMCQ(myTester.java:207)

在测试文件中,我有这个:

@Test
public void testIsCorrectMCQ() {
    System.out.println("isCorrect of MCQ");
    MCQuestion instance = new MCQuestion(1,"Capital city of Canada is", 'A',
            "Ottawa", "Vancouver", "New York", "Toronto");
    assertFalse(instance.isCorrect("B"));
    assertTrue(instance.isCorrect("A"));  // line 207

}

我的 isCorrect 方法是这样的:

@Override
public boolean isCorrect(Object guess) {

    if (guess == null)
        return false;
    if (guess instanceof String) {

        String userGuess = (String)guess;
        return (userGuess.charAt(0) == this.getAnswer());
    }

    if (guess instanceof Character) {

        Character userGuess = (Character)guess;
        return (userGuess == this.getAnswer());
    }
    else return false;
}

非常感谢任何帮助理解正在发生的事情。

编辑 1:我的 MCQuestion 源代码

public class MCQuestion extends Question {

private char answer;
private String[] options;

public MCQuestion() {

    super();
    questionType = QuestionType.MULTIPLE_CHOICE;
}

public MCQuestion(int id, String text, char answer, String... options) {

    super(id, text);
    setOptions(options);
    setAnswer(answer);
    questionType = QuestionType.MULTIPLE_CHOICE;
}

public String[] getOptions() {

    String[] getOptions = new String[this.options.length];
    System.arraycopy(this.options, 0, getOptions, 0, this.options.length);
    return getOptions;
}

public void setOptions(String... options) {

    if (options.length > 0) {
        this.options = new String[options.length];
        for (int i = 0; i < options.length; i++) {

            if (options[i].isEmpty())
                throw new IllegalArgumentException("You have nothing in this option");
            else
                this.options[i] = options[i];
        }

    }
    else throw new IllegalArgumentException("You have no options set");
}

public char getAnswer() {

    return this.answer;
}

public void setAnswer(char ans) {

    ans = Character.toLowerCase(ans);
    int index = ans - 97;
    if (Character.isLetter(ans) && index >= 0 && index < this.options.length)
        this.answer = ans;
    else throw new IllegalArgumentException(ans + " is not a valid answer option");
}

@Override
public boolean isCorrect(Object guess) {

    if (guess == null)
        return false;
    if (guess instanceof String) {

        String userGuess = (String)guess;
        return (userGuess.charAt(0) == this.getAnswer());
    }

    if (guess instanceof Character) {

        Character userGuess = (Character)guess;
        return (userGuess == this.getAnswer());
    }
    else return false;
}

@Override
public String toString() {

    String option = "";
    if (this.options.length == 0)
        option = "No options added, yet!";
    else {
        char index = 'a';
        for (String e: options)
            option += index + ") " + e + "\n";
    }

    return (super.toString() + "\n" + option);
}

}

无论出于何种原因,您在 setAnswer() 方法中执行 ans = Character.toLowerCase(ans);,然后再将其保存在 this.answer 中。这意味着当您以大写形式提供答案时 (userGuess.charAt(0) == this.getAnswer()) 将 return 为假,但将其与存储的小写字符进行比较。

根据您是否需要不区分大小写的答案,您还应该添加或删除对 isCorrect() 方法的 Character.toLowerCase() 调用。