diff --git a/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumber/books/BookStore.java b/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumber/books/BookStore.java index e5a3ceab3e..4ccfe61542 100644 --- a/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumber/books/BookStore.java +++ b/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumber/books/BookStore.java @@ -1,13 +1,9 @@ package com.baeldung.cucumber.books; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; public class BookStore { - private List books = new ArrayList<>(); public void addBook(Book book) { @@ -20,7 +16,13 @@ public class BookStore { public List booksByAuthor(String author) { return books.stream() - .filter(book -> Objects.equals(author, book.getAuthor())) - .collect(Collectors.toList()); + .filter(book -> Objects.equals(author, book.getAuthor())) + .collect(Collectors.toList()); + } + + public Optional bookByTitle(String title) { + return books.stream() + .filter(book -> book.getTitle().equals(title)) + .findFirst(); } } diff --git a/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumber/books/BookStoreRunSteps.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumber/books/BookStoreRunSteps.java index e0944d3dd4..995a3469f0 100644 --- a/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumber/books/BookStoreRunSteps.java +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumber/books/BookStoreRunSteps.java @@ -1,7 +1,5 @@ package com.baeldung.cucumber.books; -import static org.junit.Assert.assertEquals; - import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -12,10 +10,12 @@ import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import io.cucumber.datatable.DataTable; -public class BookStoreRunSteps { +import static org.junit.Assert.*; +public class BookStoreRunSteps { private BookStore store; private List foundBooks; + private Book foundBook; @Before public void setUp() { @@ -23,19 +23,22 @@ public class BookStoreRunSteps { foundBooks = new ArrayList<>(); } + @Given("^I have the following books in the store$") + public void haveBooksInTheStore(DataTable table) { + haveBooksInTheStoreByList(table); + } + @Given("^I have the following books in the store by list$") public void haveBooksInTheStoreByList(DataTable table) { - List> rows = table.asLists(String.class); - + for (List columns: rows) { store.addBook(new Book(columns.get(0), columns.get(1))); } } - + @Given("^I have the following books in the store by map$") public void haveBooksInTheStoreByMap(DataTable table) { - List> rows = table.asMaps(String.class, String.class); for (Map columns: rows) { @@ -52,9 +55,24 @@ public class BookStoreRunSteps { public void searchForBooksByAuthor(String author) { foundBooks = store.booksByAuthor(author); } + + @When("^I search for a book titled (.+)$") + public void searchForBookByTitle(String title) { + foundBook = store.bookByTitle(title).orElse(null); + } @Then("^I find (\\d+) books$") public void findBooks(int count) { assertEquals(count, foundBooks.size()); } + + @Then("^I find a book$") + public void findABook() { + assertNotNull(foundBook); + } + + @Then("^I find no book$") + public void findNoBook() { + assertNull(foundBook); + } } diff --git a/testing-modules/testing-libraries/src/test/resources/features/book-store-with-background.feature b/testing-modules/testing-libraries/src/test/resources/features/book-store-with-background.feature new file mode 100644 index 0000000000..61bcdc5c50 --- /dev/null +++ b/testing-modules/testing-libraries/src/test/resources/features/book-store-with-background.feature @@ -0,0 +1,22 @@ +Feature: Book Store With Background + Background: The Book Store + Given I have the following books in the store + | The Devil in the White City | Erik Larson | + | The Lion, the Witch and the Wardrobe | C.S. Lewis | + | In the Garden of Beasts | Erik Larson | + + Scenario: Find books by author + When I search for books by author Erik Larson + Then I find 2 books + + Scenario: Find books by author, but isn't there + When I search for books by author Marcel Proust + Then I find 0 books + + Scenario: Find book by title + When I search for a book titled The Lion, the Witch and the Wardrobe + Then I find a book + + Scenario: Find book by title, but isn't there + When I search for a book titled Swann's Way + Then I find no book \ No newline at end of file diff --git a/testing-modules/testing-libraries/src/test/resources/features/book-store-without-background.feature b/testing-modules/testing-libraries/src/test/resources/features/book-store-without-background.feature new file mode 100644 index 0000000000..771c585127 --- /dev/null +++ b/testing-modules/testing-libraries/src/test/resources/features/book-store-without-background.feature @@ -0,0 +1,32 @@ +Feature: Book Store Without Background + Scenario: Find books by author + Given I have the following books in the store + | The Devil in the White City | Erik Larson | + | The Lion, the Witch and the Wardrobe | C.S. Lewis | + | In the Garden of Beasts | Erik Larson | + When I search for books by author Erik Larson + Then I find 2 books + + Scenario: Find books by author, but isn't there + Given I have the following books in the store + | The Devil in the White City | Erik Larson | + | The Lion, the Witch and the Wardrobe | C.S. Lewis | + | In the Garden of Beasts | Erik Larson | + When I search for books by author Marcel Proust + Then I find 0 books + + Scenario: Find book by title + Given I have the following books in the store + | The Devil in the White City | Erik Larson | + | The Lion, the Witch and the Wardrobe | C.S. Lewis | + | In the Garden of Beasts | Erik Larson | + When I search for a book titled The Lion, the Witch and the Wardrobe + Then I find a book + + Scenario: Find book by title, but isn't there + Given I have the following books in the store + | The Devil in the White City | Erik Larson | + | The Lion, the Witch and the Wardrobe | C.S. Lewis | + | In the Garden of Beasts | Erik Larson | + When I search for a book titled Swann's Way + Then I find no book \ No newline at end of file