...

/

Let's Write Tests

Let's Write Tests

We will learn how to write tests that cover one loop and examine them to understand how to make them fit for other use cases.

The bulk of the “interesting” logic in matches() resides in the body of the for loop in the Profile class.

Press + to interact
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;
}

First test: Covering one path

Let’s write a simple test that covers one path through the loop.

Two points that are evident from the code:

  • We need a Profile instance.
  • We need a Criteria object to pass as an argument to matches().

By analyzing the code in matches() and looking at the constructors of Criteria, Criterion, and Question, we can figure out how to piece together a useful Criteria object.

The analysis lets us write this part of the arranged portion of the test. Press the Run button below to execute the test code.: ...

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