Understanding What We’re Testing: The Profile Class
Learn to test the profile class by understanding the conditions one can write tests for.
We'll cover the following...
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?” AProfilefor that job seeker may contain anAnswerobject with the valuetruefor that question. -
We add
Answerobjects to aProfileby using theadd()method (line 18). AQuestioncontains the text of a question plus the allowable range of answers (true or false for yes/no questions). TheAnswerobject references the correspondingQuestionand contains an appropriate value for theanswer(line 29). -
A
Criteriainstance (see line 22) is simply a container that holds a bunch ofCriterions. ACriterion(first referenced on line 27) represents what an employer seeks in an employee or vice versa. It encapsulates anAnswerobject and aWeight...