Extract and override call

We will apply the already introduced refactoring technique extract and override call. For this, we create a failing specification, as shown here:

@Test 
public void add_one_book() throws IOException { 
    addBook(TDD_IN_JAVA); 
 
    Book tddInJava = new Book(TITLE_BOOK_1, 
      AUTHOR_BOOK_1, 
       States.fromValue(1)); 
 
    verify(booksRepository).add(tddInJava); 
} 

To pass this red specification, also known as a failing specificiation, we will first extract the dependency creation to a protected method in the BookRepository class:

@Path("books") 
@Component 
public class BooksEndpoint { 
 
    private BooksRepository books = 
      getBooksRepository(); 
 
    [...] 
 
     protected BooksRepository 
       getBooksRepository() { 
        return new BooksRepository(); 
    } 
 
    [...] 

We copy ...

Get Test-Driven Java Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.