Java 当方法在jUnit中抛出异常时
对于在jUnit测试中抛出异常的方法,您会怎么做?如您所见,Java 当方法在jUnit中抛出异常时,java,exception,jakarta-ee,junit,Java,Exception,Jakarta Ee,Junit,对于在jUnit测试中抛出异常的方法,您会怎么做?如您所见,Question类中的addAnswer方法可能会引发异常。在shouldFaildIftwoAnswerAreCorrect方法中,我想检查是否引发异常,但在shouldAddAnswersToQuestion 我应该从privateaddAnswerToQuestion方法中添加throwsMultipleAnswersAreCorrectException,然后在应该添加answerstoquestion中尝试/捕获,还是也将其抛
Question
类中的addAnswer
方法可能会引发异常。在shouldFaildIftwoAnswerAreCorrect
方法中,我想检查是否引发异常,但在shouldAddAnswersToQuestion
我应该从privateaddAnswerToQuestion
方法中添加throwsMultipleAnswersAreCorrectException
,然后在应该添加answerstoquestion
中尝试/捕获,还是也将其抛出该方法
当方法在测试中抛出异常时,您会怎么做
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);
}
}
您必须将
抛出
声明添加到addAnswerToQuestion
,然后尝试/捕获异常或使用预期的
属性或@Test
:
@Test(expected=IOException.class)
public void test() {
// test that should throw IOException to succeed.
}
在
shouldAddAnswers to question
测试中,如果您不希望出现MultipleanswersRecorrectException
异常,则可以在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");
}
}
我认为最好是让测试失败,而不是从测试中抛出异常,因为所有测试都应该因为断言失败而失败,而不是因为异常。那么,在jUnit测试中,除了正常的try/catch之外,您不做任何事情了吗?如果由于某种奇怪的原因抛出异常(即使在这种方法中不应该抛出异常),您该怎么办?您是否编写.printStackTrace()等?完全正确。因此,只要不捕获,然后就可以让堆栈跟踪开始调试。@他在理解您的意思时有点困难。你的意思是我不应该在catch中做任何事情,还是我应该打印stacktrace?绝对不是!这样,测试失败的所有信息都将完全丢失。否则,您可以查看堆栈跟踪。测试框架知道如何处理抛出异常的方法。@他的这个异常是一个用户定义的异常,因此没有什么可调试的,我们应该知道何时会引发这个异常,所以没有必要打印stacktrace。测试应该只有两个输出,要么通过,要么失败。您不应该打印stacktrace。意外异常是失败的。我们知道此异常可能发生的两件事,第二件事异常不同于测试断言失败此异常是否在测试中发生?将其添加为预期的异常。不应该发生吗?测试失败。这可能发生吗?您没有编写单元测试。可能是的重复项