Supporting Multiple Answers: A Small Design Detour
Learn how to modify the classes and tests to support multiple answers.
We'll cover the following
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 aMap
(line 2) where the key is thequestion
text and the value is the associatedAnswer
.
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.