Search⌘ K
AI Features

Supporting Multiple Answers: A Small Design Detour

Explore how to effectively handle multiple answers in unit tests for Java applications using JUnit. Understand techniques for storing answers in collections, managing null values with guard clauses, and refactoring test code to improve clarity and design. This lesson guides you through practical refactoring steps and test case adjustments to support complex test scenarios.

Scenario: Profile’s multiple answers

A profile can contain many answers, which the test tackles in this scenario. Here is the code for ProfileTest:

@Test
   public void matchesWhenContainsMultipleAnswers() {
      profile.add(answerThereIsRelocation);
      profile.add(answerDoesNotReimburseTuition);
      Criterion criterion = 
            new Criterion(answerThereIsRelocation, Weight.Important);
      
      boolean result = profile.matches(criterion);
      
      assertTrue(result);
   }

Tackling strategy

...