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

时间:2018-03-25 16:58:55

标签: java junit stack-trace

完全披露:这是一项任务,已经标记,但我想了解为什么我会收到此错误。

我有一些问题需要理解为什么会抛出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);
}

}

1 个答案:

答案 0 :(得分:0)

您在ans = Character.toLowerCase(ans);方法中出于某种原因执行setAnswer(),然后将其保存在this.answer中。这意味着当您以大写字母提供答案时,(userGuess.charAt(0) == this.getAnswer())将返回false,但会将其与存储的小写字符进行比较。

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

相关问题