junit.framework.AssertionfailedError-给出的不完整堆栈跟踪



完整披露:这是一个分配,已经被标记了,但是我想了解为什么我会遇到此错误。

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

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

}

在将其保存在this.answer中之前,您出于setAnswer()方法中的任何原因执行ans = Character.toLowerCase(ans);。这意味着当您在大量情况下提供答案时,(userGuess.charAt(0) == this.getAnswer())将返回false,但将其与存储的下部案例字符进行比较。

根据您是否需要案例不敏感的答案,也应将Character.toLowerCase()调用添加或删除isCorrect()方法。

最新更新