Supporting Multiple Answers: A Small Design Detour

Learn how to modify the classes and tests to support multiple answers.

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

Having multiple Answers in the Profile requires a way to store and distinguish them.

In this scenario, we have chosen to store the Answers in a Map (line 2) where the key is the question text and the value is the associated Answer.

It’d probably be better to use an Answer ID as the key, but Answer has no such thing yet.

Get hands-on with 1400+ tech skills courses.