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.:

package iloveyouboss;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.*;
import org.junit.Test;

public class ProfileTest {

   @Test
   public void test() {
      Profile profile = new Profile("Bull Hockey, Inc.");
      Question question = new BooleanQuestion(1, "Got bonuses?");
      Criteria criteria = new Criteria();
      Answer criteriaAnswer = new Answer(question, Bool.TRUE);
      Criterion criterion = new Criterion(criteriaAnswer, Weight.MustMatch);
      criteria.add(criterion);
   }
}
ProfileTest.java file

Hurrah! The output Ok (1) shows that the test has passed, and Time shows the ...