【问题标题】:When methods are throwing exceptions in jUnit当方法在 jUnit 中抛出异常时
【发布时间】:2012-02-19 15:07:09
【问题描述】:

您如何处理在 jUnit 测试中引发异常的方法?如您所见,Question 类中的addAnswer 方法可能会引发异常。在shouldFailifTwoAnswerAreCorrect 方法中我想检查是否抛出异常,但在shouldAddAnswersToQuestion

我应该从私有 addAnswerToQuestion 方法中添加 throws MultipleAnswersAreCorrectException 并在 shouldAddAnswersToQuestion 中尝试/捕获,还是也应该在该方法中抛出它?

当方法在测试中抛出异常时你会怎么做?

public class QuestionTest {

    private Question question;

    @Before
    public void setUp() throws Exception {
        question = new Question("How many wheels are there on a car?", "car.png");
    }

    @Test
    public void shouldAddAnswersToQuestion() {

        addAnswerToQuestion(new Answer("It is 3", false));
        addAnswerToQuestion(new Answer("It is 4", true));
        addAnswerToQuestion(new Answer("It is 5", false));
        addAnswerToQuestion(new Answer("It is 6", false));

        assertEquals(4, question.getAnswers().size());
    }

    @Test(expected = MultipleAnswersAreCorrectException.class)
    public void shouldFailIfTwoAnswersAreCorrect() {

        addAnswerToQuestion(new Answer("It is 3", false));
        addAnswerToQuestion(new Answer("It is 4", true));
        addAnswerToQuestion(new Answer("It is 5", true));
        addAnswerToQuestion(new Answer("It is 6", false));
    }

    private void addAnswerToQuestion(Answer answer) {
        question.addAnswer(answer);
    }
}

问题类中的方法

public void addAnswer(Answer answer) throws MultipleAnswersAreCorrectException {

    boolean correctAnswerAdded = false;

    for (Answer element : answers) {
        if (element.getCorrect()) {
            correctAnswerAdded = true;
        }
    }

    if (correctAnswerAdded) {
        throw new MultipleAnswersAreCorrectException();
    } else {
        answers.add(answer);    
    }
}

【问题讨论】:

标签: java exception jakarta-ee junit


【解决方案1】:

您必须将throws 声明添加到addAnswerToQuestion,然后尝试/捕获异常或使用expected 属性或@Test

@Test(expected=IOException.class)
public void test() {
    // test that should throw IOException to succeed.
}

【讨论】:

  • 那么你在 jUnit 测试中的 try/catch 中除了正常之外什么都不做?如果由于奇怪的原因引发异常(即使在此方法中不应该),您会怎么做?你会写 .printStackTrace() 等吗?
  • 没错。所以不要抓住然后你得到堆栈跟踪来开始调试。
  • @his 理解你的意思有点麻烦。你的意思是我不应该在 catch 中做任何事情,或者我应该打印堆栈跟踪?
【解决方案2】:

shouldAddAnswersToQuestion 测试中,如果您不期待MultipleAnswersAreCorrectException,那么您可以将块包围在 try/catch 中并编写断言失败条件,例如

    @Test
public void shouldAddAnswersToQuestion() {
  try{
       addAnswerToQuestion(new Answer("It is 3", false));
       addAnswerToQuestion(new Answer("It is 4", true));
       addAnswerToQuestion(new Answer("It is 5", false));
       addAnswerToQuestion(new Answer("It is 6", false));
       assertEquals(4, question.getAnswers().size());
   }catch(MultipleAnswersAreCorrectException  e){
     Assert.assertFail("Some self explainable failure statement");
   }
}

我认为让测试失败总是更好,而不是从测试中抛出异常,因为所有测试都应该因为断言失败而不是因为异常而失败。

【讨论】:

  • 绝对不是!这样,测试失败的所有信息就完全丢失了。否则,您只需查看堆栈跟踪即可。测试框架知道如何处理抛出异常的方法。
  • @his 这个异常是用户定义的异常,所以没有什么要调试的,我们应该知道什么时候会引发这个异常,所以打印堆栈跟踪没有意义。测试应该只有 2 个输出通过或失败。
  • 您应该打印堆栈跟踪。意外异常失败。
  • 两件事我们知道这个异常可能会发生,第二件事异常不同于测试断言失败
  • 这个异常是否应该在这个测试中发生?将其添加为预期的异常。它不应该发生吗?测试失败。它可能发生吗?您不是在编写单元测试。
猜你喜欢
  • 1970-01-01
  • 2021-07-17
  • 2021-02-08
  • 2012-08-10
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多