Understanding What We’re Testing: The Profile Class
Learn to test the profile class by understanding the conditions one can write tests for.
Let’s look at a core class in iloveyouboss
, the Profile
class:
package iloveyouboss;import java.util.*;public class Profile {private Map<String,Answer> answers = new HashMap<>();private int score;private String name;public Profile(String name) {this.name = name;}public String getName() {return name;}public void add(Answer answer) {answers.put(answer.getQuestionText(), answer);}public boolean matches(Criteria criteria) {score = 0;boolean kill = false;boolean anyMatches = false;for (Criterion criterion: criteria) {Answer answer = answers.get(criterion.getAnswer().getQuestionText());boolean match =criterion.getWeight() == Weight.DontCare ||answer.match(criterion.getAnswer());if (!match && criterion.getWeight() == Weight.MustMatch) {kill = true;}if (match) {score += criterion.getWeight().getValue();}anyMatches |= match;}if (kill)return false;return anyMatches;}public int score() {return score;}}
The Profile class
This looks like code we come across often! Let’s walk through it.
-
A
Profile
(line 5) captures answers to relevant questions one might ask about a company or a job seeker. For example, a company might ask a job seeker, “Are you willing to relocate?” AProfile
for that job seeker may contain anAnswer
object with the valuetrue
for that question. -
We add
Answer
objects to aProfile
by using theadd()
method (line 18). AQuestion
contains the text of a question plus the allowable range of answers (true or false for yes/no questions). TheAnswer
object references the correspondingQuestion
and contains an appropriate value for theanswer
(line 29). -
A
Criteria
instance (see line 22) is simply a container that holds a ...