Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finish exercise #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public Library(Inventory inventory) {
}

public Book checkOut(String studentId, String isbn) throws BookNotAvailableException {

if (!inventory.isBookAvailable(isbn)) {
throw new BookNotAvailableException(isbn);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public void countingWordsOfEmptyBookReturnsZero() {

@Test
public void countingWordsReturnsNumberOfWordsInBook() {
assertEquals(0, 1); // Replace this line with the actual test code...
// Given
Book book = new Book("someISBN", "this is the content");

// When
double wordCount = BookStats.countWords(book);

// Then
assertEquals(4, wordCount);
}
}
36 changes: 36 additions & 0 deletions school-library/src/test/java/com/redhat/training/LibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,40 @@ public void setUp() {
}

// Add tests here...
@Test
public void checkingOutDecreasesNumberOfBookCopiesFromInventory()
throws BookNotAvailableException {
// Given
inventory.add(new Book("book1"));
inventory.add(new Book("book1"));

// When
library.checkOut("someStudentId", "book1");

// Then
assertEquals(1, inventory.countCopies("book1"));
}

@Test
public void checkingOutUnavailableBookThrowsException()
throws BookNotAvailableException {
// Given
inventory.add(new Book("book1"));
inventory.add(new Book("book1"));

library.checkOut("student1", "book1");
library.checkOut("student2", "book1");

// When
final BookNotAvailableException exception = assertThrows(
BookNotAvailableException.class,
() -> {
library.checkOut("student3", "book1");
}
);

// Then
assertTrue(exception.getMessage().matches("Book book1 is not available"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,35 @@ public void setUp() {
}

// Add tests here...
@Test
public void checkingOutWithdrawsFromInventoryWhenBookIsAvailable()
throws BookNotAvailableException {
// Given
when(inventory.isBookAvailable("book1")).thenReturn(true);

// When
library.checkOut("student1", "book1");

// Then
verify(inventory).withdraw("book1");
}

@Test
public void checkingOutDoesNotWithdrawFromInventoryWhenBookIsUnavailable()
throws BookNotAvailableException {
// Given
when(inventory.isBookAvailable("book1")).thenReturn(false);

// When
try {
library.checkOut("student1", "book1");
} catch(BookNotAvailableException e) {}

// Then
verify(inventory, times(0)).withdraw("book1");

}



}