@Test public void shouldReportErrorMessageWhenUserSelectsABookThatDoesNotExist() throws IOException { when(bufferedReader.readLine()).thenReturn("2", "100", "1", "0"); when(library.checkOutBook(99)).thenReturn(false); when(library.checkOutBook(0)).thenReturn(true); bookList.add(harryPotter); menu.respondToUserInput(library); verify(printStream).println(contains("That book is not available.")); }
private Library buildMockLibraryWithSetBookAmounts( int numberOfBooksAvailable, int numberOfBooksCheckedOut) { library = mock(Library.class); when(library.numberOfBooksInLibrary()).thenReturn(numberOfBooksAvailable); when(library.numberOfBooksCheckedOut()).thenReturn(numberOfBooksCheckedOut); for (int i = 0; i < numberOfBooksAvailable; i++) { when(library.bookIsAvailable(i)).thenReturn(true); } for (int i = 0; i < numberOfBooksCheckedOut; i++) { when(library.bookIsCheckedOut(i)).thenReturn(true); } return library; }
@Test public void shouldPromptUserForBookTitleTheyWantToReturn() throws IOException { when(bufferedReader.readLine()).thenReturn("3", "SOME TITLE!", "0"); when(library.returnBook(anyString())).thenReturn(true); menu.respondToUserInput(library); verify(printStream).println("Which book would you like to return?"); }
@Test public void testWarnsUserIfNoBooksAreAvailable() { when(library.numberOfBooksInLibrary()).thenReturn(0); checkoutBookMenuOperation.execute(library, console); verify(console).printMessage("There are no books available for checkout"); }
@Test public void shouldPromptUserWhenFailedReturnBook() throws IOException { when(bufferedReader.readLine()).thenReturn("3", "1"); when(library.returnBook(anyString())).thenReturn(false, true); menu.respondToUserInput(library); verify(printStream).println("That is not a valid book to return."); }
@Test public void shouldThankUserWhenSuccessfulReturnOfBook() throws IOException { when(bufferedReader.readLine()).thenReturn("3", "1"); when(library.returnBook(anyString())).thenReturn(true); menu.respondToUserInput(library); verify(printStream).println("Thank you for returning the book."); }
@Test public void shouldCallReturnToLibraryWithCorrectBook() throws IOException { when(bufferedReader.readLine()).thenReturn("3", "SOME BOOK TITLE!", "0"); when(library.returnBook(anyString())).thenReturn(true); menu.respondToUserInput(library); verify(library).returnBook("SOME BOOK TITLE!"); }
@Test public void shouldPrintNothingWhenThereAreNoBooks() { createBooksPrintSteamAndLibrary(); library.listBooks(); verifyZeroInteractions(printStream); }
// This one is done for you @Test public void shouldWelcomeUser() { // We don't need to mock DateTime because it is a value object // We can't mock it because it is a final class createBooksPrintStreamDateTimeAndLibrary(); library.welcome(time); verify(printStream).println(contains("Welcome")); }
@Test public void shouldDisplayFormattedTimeWhenFormattedTimeIsAnEmptyString() { createBooksPrintStreamDateTimeAndLibrary(); when(dateTimeFormatter.print(time)).thenReturn(""); library.welcome(time); verify(printStream).println(contains("The current time is ")); }
@Test public void shouldAllowUserToReenterReturningBookTitle() throws IOException { when(bufferedReader.readLine()).thenReturn("3", "1"); when(library.returnBook(anyString())).thenReturn(false, true); menu.respondToUserInput(library); verify(printStream).println("That is not a valid book to return."); verify(printStream).println("Thank you for returning the book."); }
@Test public void cannotRemoveFormatAttributeWithNullName() { try { Library.newBuilder().removeFormatAttribute(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getKey("nullFormat")); } }
@Test public void cannotAddNullFormatAttribute() { try { Library.newBuilder().addFormatAttribute("foo", null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getKey("nullAttribute")); } }
@Test public void cannotRemoveNullKeyword() { try { Library.newBuilder().removeKeyword(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getKey("nullName")); } }
@Test public void shouldPrintBookTitleWhenThereIsOneBook() { createBooksPrintSteamAndLibrary(); String title = "Book Title"; books.add(title); library.listBooks(); // add a verify statement here that shows that the book title was printed by to the printStream verify(printStream).println("Book Title"); }
@Test public void shouldPrintBothBookTitlesWhenThereAreTwoBooks() { createBooksPrintSteamAndLibrary(); String title = "Book Title"; String title2 = "Book Title 2"; books.add(title); books.add(title2); library.listBooks(); verify(printStream).println("Book Title"); verify(printStream).println("Book Title 2"); }
@Test public void testAvailableBookListPrintedOnExecute() { checkoutBookMenuOperation.execute(library, console); verify(console).printLibraryItemList(library.availableBooks()); }
private void successfullyReturnBook() throws IOException { when(bufferedReader.readLine()).thenReturn("2", "1", "0"); when(library.checkOutBook(0)).thenReturn(true); }