Search⌘ K

Let's Write Tests

Explore writing foundational unit tests in JUnit by creating test cases that cover various code paths in Java methods. Understand how to organize test data and assert expected outcomes while maintaining clarity and reducing redundancy across tests.

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

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