The Value of Focused, Single-Purpose Tests

The tests we built in Chapter 1, Building Your First JUnit Test are short—four lines of code each. We might consider combining them:

iloveyouboss/12/test/iloveyouboss/ProfileTest.java
 
@Test
 
public​ ​void​ matches() {
 
Profile profile = ​new​ Profile(​"Bull Hockey, Inc."​);
 
Question question = ​new​ BooleanQuestion(1, ​"Got milk?"​);
 
 
// answers false when must-match criteria not met
 
profile.add(​new​ Answer(question, Bool.FALSE));
 
Criteria criteria = ​new​ Criteria();
 
criteria.add(
 
new​ Criterion(​new​ Answer(question, Bool.TRUE), Weight.MustMatch));
 
 
assertFalse(profile.matches(criteria));
 
 
// answers true for any don't care criteria
 
profile.add(​new​ Answer(question, ...

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.