Skip to content

Commit

Permalink
[BAEL-3445] Added Background examples to Cucumber examples (eugenp#8319)
Browse files Browse the repository at this point in the history
  • Loading branch information
François Dupire authored and maibin committed Dec 6, 2019
1 parent 957cfeb commit 4394a14
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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<Book> books = new ArrayList<>();

public void addBook(Book book) {
Expand All @@ -20,7 +16,13 @@ public void addAllBooks(Collection<Book> books) {

public List<Book> 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<Book> bookByTitle(String title) {
return books.stream()
.filter(book -> book.getTitle().equals(title))
.findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,30 +10,35 @@
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<Book> foundBooks;
private Book foundBook;

@Before
public void setUp() {
store = new BookStore();
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<List<String>> rows = table.asLists(String.class);

for (List<String> 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<Map<String, String>> rows = table.asMaps(String.class, String.class);

for (Map<String, String> columns: rows) {
Expand All @@ -52,9 +55,24 @@ public void haveBooksInTheStoreByTransformer(BookCatalog catalog) {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4394a14

Please sign in to comment.