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

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 ...

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