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 its final state:
public class BooksEndpointInteractionTest { public static final URI FULL_PATH = URI. create("http://localhost:8080/alexandria"); private HttpServer server; private BooksRepository booksRepository; @Before public void setUp() throws IOException { booksRepository = mock(BooksRepository.class); BooksEndpoint booksEndpoint = new BooksEndpoint(booksRepository); ResourceConfig resourceConfig = new MyApplication(booksEndpoint); server = GrizzlyHttpServerFactory .createHttpServer(FULL_PATH, resourceConfig); server.start(); }
The first test passed, so we can mark the dependency injection task as done.
Tasks performed:
- Dependency injection in BooksEndpoint for books
The to-do list:
- Change status for statuses
- Remove the primitive obsession with status (optional)