O'Reilly logo

Test-Driven Java Development - Second Edition by Viktor Farcic, Alex Garcia

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding a new feature

Once we have the necessary test environment in place, we can add the new feature.

As a library manager, I want to know all the history for a given book so that I can measure which books are more in demand than others.

We will start with a red specification:

public class BooksSpec { 
 
    @Test 
    public void should_search_for_any_past_state() { 
        Book book1 = new Book("title", "author",            States.AVAILABLE); 
        book1.censor(); 
 
        Books books = new Books(); 
        books.add(book1); 
 
        String available = 
          String.valueOf(States.AVAILABLE); 
        assertThat( 
          books.filterByState(available).isEmpty(),            is(false)); 
    } 
} 

Run all the tests and see the last one fail.

Implement the search on all states (fragment):

public class Book { private ArrayList<Integer> status; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required