Commit 72f30e7b authored by earndt's avatar earndt

[W6D2] (ArndtED) Adds working BookStore test

parent dcecf319
package hellocucumber;
public class Book {
private String title;
private String author;
public Book(String title, String author){
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
}
package hellocucumber;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
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());
}
}
package hellocucumber;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.junit.Before;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
class BookStoreTest {
static String bookStore(String bookStore){
return null;
}
}
public class BookStoreSteps {
private BookStore store = new BookStore();
private List<Book> foundBooks;
@Before
public void setUp(){
foundBooks = new ArrayList<>();
}
@Given("^The following books are in the bookstore$")
public void the_following_books_are_in_the_bookstore(DataTable data){
List<Map<String, String>> rows = data.asMaps(String.class, String.class);
for(Map<String, String> columns : rows) {
store.addBook(new Book(columns.get("title"), columns.get("author")));
}
}
@When("^I search for books by author Neal Stephenson$")
public void i_search_for_books_by_author_neal_stephenson() throws Throwable {
foundBooks = store.booksByAuthor("Neal Stephenson");
}
@Then("^I find (\\d+) books$")
public void i_find_books(int arg1) throws Throwable {
Assert.assertEquals(arg1, foundBooks.size());
}
}
......@@ -5,7 +5,11 @@ import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"})
@CucumberOptions(plugin = {"pretty",
"html:target/surefire-reports/html",
"json:target/surefire-reports/json/report.json",
"junit:target/surefire-reports/junit/report.xml"},
monochrome = true)
public class RunCucumberTest {
}
Feature: Book Store
Scenario: Find correct nonzero number of books by author
Given The following books are in the bookstore
| title | author |
| Anathem | Neal Stephenson |
| The Beach | Alex Garland |
| Snow Crash | Neal Stephenson |
When I search for books by author Neal Stephenson
Then I find 2 books
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment