Parameterizing a constructor

We can unify the launchers by accepting a BooksEndpoint dependency. If we don't specify it, it will register the dependency with the real instance of BooksRepository. Otherwise, it will register the received one:

public class MyApplication 
      extends ResourceConfig { 
 
    public MyApplication() { 
        this(new BooksEndpoint( 
          new BooksRepository())); 
    } 
 
    public MyApplication 
      (BooksEndpoint booksEndpoint) { 
        register(booksEndpoint); 
        register(RequestContextFilter.class); 
        register(JacksonJaxbJsonProvider.class); 
        register(CustomExceptionMapper.class); 
    } 
} 

In this case, we have opted for constructor chaining to avoid repetition in the constructors.

After doing this refactor, the BooksEndpointInteractionTest class is as follows in ...

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.