Search⌘ K

Understanding What We’re Testing: The Profile Class

Explore how to analyze and test the Profile class in Java with JUnit by understanding its criteria matching method. Learn to identify important test conditions, handle data variants, and focus on effective test case design rather than exhaustive testing.

Let’s look at a core class in iloveyouboss, the Profile class:

Java
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 bunch of Criterions. A Criterion (first referenced on line 27) represents what an employer seeks in an employee or vice versa. It encapsulates an Answer object and a Weight ...