dupirefr/dupire.francois+pro@gmail.com [BAEL-3445] Cucumber Backgrounds (Moved package) (#8350)

* [BAEL-3445] Added Background examples to Cucumber examples

* [BAEL-3445] Copied code to have a package dedicated to the article

* [BAEL-3445] Removed code from other package
This commit is contained in:
François Dupire 2019-12-11 19:25:03 +01:00 committed by maibin
parent dfb9ae68a6
commit c8cafe8cd2
7 changed files with 145 additions and 11 deletions

View File

@ -19,10 +19,4 @@ public class BookStore {
.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();
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.cucumberbackground.books;
public class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public Book() {}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [title=" + title + ", author=" + author + "]";
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.cucumberbackground.books;
import java.util.*;
import java.util.stream.Collectors;
public class BookStore {
private List<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
}
public void addAllBooks(Collection<Book> books) {
this.books.addAll(books);
}
public List<Book> booksByAuthor(String author) {
return books.stream()
.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();
}
}

View File

@ -56,11 +56,6 @@ public class BookStoreRunSteps {
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());

View File

@ -0,0 +1,58 @@
package com.baeldung.cucumberbackground.books;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import java.util.ArrayList;
import java.util.List;
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) {
List<List<String>> rows = table.asLists(String.class);
for (List<String> columns: rows) {
store.addBook(new Book(columns.get(0), columns.get(1)));
}
}
@When("^I search for books by author (.+)$")
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);
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.cucumberbackground.books;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features/book-store-with-background.feature")
public class BookStoreWithBackgroundIntegrationTest {
}

View File

@ -0,0 +1,12 @@
package com.baeldung.cucumberbackground.books;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features/book-store-without-background.feature")
public class BookStoreWithoutBackgroundIntegrationTest {
}