Covering One Path

The bulk of the “interesting” logic in matches() resides in the body of the for loop. Let’s write a simple test that covers one path through the loop.

Two points that glancing at the code should make obvious: we need a Profile instance, and we need a Criteria object to pass as an argument to matches().

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

The analysis lets you write this part of the arrange portion of the test:

iloveyouboss/6/test/iloveyouboss/ProfileTest.java
 
@Test
 
public​ ​void​ test() {
 
Profile profile = ​new​ Profile(​"Bull Hockey, Inc."​);
 
Question question = ​new​ BooleanQuestion(1, ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.