...

/

Understanding What We’re Testing: The Profile Class

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:

Press + to interact
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?” A Profile for that job seeker may contain an Answer object with the value true for that question.

  • We add Answer objects to a Profile by using the add() method (line 18). A Question contains the text of a question plus the allowable range of answers (true or false for yes/no questions). The Answer object references the corresponding Question and contains an appropriate value for the answer (line 29).

  • A Criteria instance (see line 22) is simply a container that holds a ...

Access this course and 1400+ top-rated courses and projects.