[BAEL-3445] Added Background examples to Cucumber examples (#8319)
This commit is contained in:
parent
69f8d56f3e
commit
8121161b36
@ -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) {
|
||||
@ -20,7 +16,13 @@ public class BookStore {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -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<Book> 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<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) {
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user