I have made one test that checks if the answers on a question is added properly. I also have a update method test. But as you see in the update method you see that I check if the answer has been added before doing what the test is really supposed to do. Is this wrong? Shoud I assume that it works since I already have the case of adding answers covered by another test or should I make such asserts?
@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) {
e.printStackTrace();
}
}
@Test
public void shouldUpdateAnswerInQuestion() {
try {
Answer answer = new Answer("It is 4", true);
addAnswerToQuestion(answer);
Answer answerFromList = null;
answerFromList = question.getAnswers().get(0);
assertEquals(answer, answerFromList);
answer.setDescription("It is now 5 instead of 4");
question.updateAnswer(answer);
answerFromList = question.getAnswers().get(0);
assertEquals(answer, answerFromList);
} catch (MultipleAnswersAreCorrectException e) {
e.printStackTrace();
}
}
A test should only assert on one bit of observable behaviour. Other behaviours that the test incidentally touches should generally be assumed to be correct, because we test for them elsewhere.
“One bit of observable behaviour” does not mean that you should only have a single assertion, but that you should only assert on one “thing” or one “unit of behaviour”, for some value of thing or behaviour.
If I had written the
shouldUpdateAnswerInQuestiontest, then I would not have added the first assertion.