private ModelAndView bookPageModelAndView(String ID, boolean isValidID) throws IOException { BookService bookService = mock(BookService.class); Book book = (isValidID) ? getBook() : null; when(bookService.getBookByID(Integer.parseInt(ID))).thenReturn(book); BookViewController bookViewController = new BookViewController(bookService); return bookViewController.viewBook(ID, ""); }
@Test public void shouldVerifyThatBookServiceReturnsBook() { Book book = getBook(); BookService bookService = mock(BookService.class); when(bookService.getBookByID(3)).thenReturn(book); BookViewController bookViewController = new BookViewController(bookService); bookViewController.recommend(3); verify(bookService).getBookByID(3); }
@Test public void shouldHaveEmptyNotificationWhenUserViewsBookForFirstTime() { BookService bookService = mock(BookService.class); Book book = getBook(); when(bookService.getBookByID(book.getId())).thenReturn(book); BookViewController bookViewController = new BookViewController(bookService); ModelAndView viewBook = bookViewController.viewBook(Integer.toString(book.getId()), ""); String actualNotification = (String) viewBook.getModel().get("notification"); assertThat(actualNotification, is("")); }
@Test public void shouldPassANotificationToTheViewThatBookWasRecommended() { BookService bookService = mock(BookService.class); Book book = getBook(); when(bookService.getBookByID(0)).thenReturn(book); BookViewController bookViewController = new BookViewController(bookService); RedirectView viewBook = bookViewController.recommend(0); String actualNotification = viewBook.getUrl(); assertThat( actualNotification, is( "/viewbook?bookId=" + book.getId() + "¬ification=" + BookViewController.RECOMMENDED_SUCCESFULLY)); }