Cleaning Up Our Tests

After the second pass through the TDD cycle, we have code we can clean up. Not in the Profile class, but in the tests. We want the tests to stay short and clear. Both our tests instantiate Profile. Create a Profile field and move the common initialization to an @Before method:

iloveyouboss/tdd-6/test/iloveyouboss/ProfileTest.java
 
public​ ​class​ ProfileTest {
*
private​ Profile profile;
 
*
@Before
*
public​ ​void​ createProfile() {
*
profile = ​new​ Profile();
*
}
 
*
@Test
*
public​ ​void​ matchesNothingWhenProfileEmpty() {
*
Question question = ​new​ BooleanQuestion(1, ​"Relocation package?"​);
*
Criterion criterion =
*
new​ Criterion(​new​ Answer(question, Bool.TRUE), Weight.DontCare);
*
*
boolean​ result = profile.matches(criterion); ...

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.