Merge pull request #8195 from albanoj2/BAEL-3444
BAEL-3444: Cucumber Data Table
This commit is contained in:
commit
442d82ac66
|
@ -18,19 +18,20 @@
|
|||
<version>${lambda-behave.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>info.cukes</groupId>
|
||||
<groupId>io.cucumber</groupId>
|
||||
<artifactId>cucumber-junit</artifactId>
|
||||
<version>${cucumber.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>info.cukes</groupId>
|
||||
<groupId>io.cucumber</groupId>
|
||||
<artifactId>cucumber-java</artifactId>
|
||||
<version>${cucumber.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>info.cukes</groupId>
|
||||
<groupId>io.cucumber</groupId>
|
||||
<artifactId>cucumber-java8</artifactId>
|
||||
<version>${cucumber.version}</version>
|
||||
<scope>test</scope>
|
||||
|
@ -87,7 +88,7 @@
|
|||
|
||||
<properties>
|
||||
<lambda-behave.version>0.4</lambda-behave.version>
|
||||
<cucumber.version>1.2.5</cucumber.version>
|
||||
<cucumber.version>4.8.0</cucumber.version>
|
||||
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.cucumber.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 + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.cucumber.books;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BookCatalog {
|
||||
|
||||
private List<Book> books = new ArrayList<>();
|
||||
|
||||
public void addBook(Book book) {
|
||||
books.add(book);
|
||||
}
|
||||
|
||||
public List<Book> getBooks() {
|
||||
return books;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BookCatalog [books=" + books + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.cucumber.books;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
package com.baeldung.calculator;
|
||||
|
||||
import cucumber.api.CucumberOptions;
|
||||
import cucumber.api.junit.Cucumber;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import io.cucumber.junit.Cucumber;
|
||||
import io.cucumber.junit.CucumberOptions;
|
||||
|
||||
@RunWith(Cucumber.class)
|
||||
@CucumberOptions(
|
||||
features = {"classpath:features/calculator.feature", "classpath:features/calculator-scenario-outline.feature"}
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
package com.baeldung.calculator;
|
||||
|
||||
import com.baeldung.cucumber.Calculator;
|
||||
import cucumber.api.java.Before;
|
||||
import cucumber.api.java.en.Given;
|
||||
import cucumber.api.java.en.Then;
|
||||
import cucumber.api.java.en.When;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.baeldung.cucumber.Calculator;
|
||||
|
||||
import io.cucumber.java.Before;
|
||||
import io.cucumber.java.en.Given;
|
||||
import io.cucumber.java.en.Then;
|
||||
import io.cucumber.java.en.When;
|
||||
|
||||
public class CalculatorRunSteps {
|
||||
|
||||
private int total;
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.cucumber.books;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import io.cucumber.junit.Cucumber;
|
||||
import io.cucumber.junit.CucumberOptions;
|
||||
|
||||
@RunWith(Cucumber.class)
|
||||
@CucumberOptions(features = "classpath:features/book-store.feature")
|
||||
public class BookStoreIntegrationTest {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.cucumber.books;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import io.cucumber.core.api.TypeRegistry;
|
||||
import io.cucumber.core.api.TypeRegistryConfigurer;
|
||||
import io.cucumber.datatable.DataTable;
|
||||
import io.cucumber.datatable.DataTableType;
|
||||
import io.cucumber.datatable.TableTransformer;
|
||||
|
||||
public class BookStoreRegistryConfigurer implements TypeRegistryConfigurer {
|
||||
|
||||
@Override
|
||||
public Locale locale() {
|
||||
return Locale.ENGLISH;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureTypeRegistry(TypeRegistry typeRegistry) {
|
||||
typeRegistry.defineDataTableType(
|
||||
new DataTableType(BookCatalog.class, new BookTableTransformer())
|
||||
);
|
||||
}
|
||||
|
||||
private static class BookTableTransformer implements TableTransformer<BookCatalog> {
|
||||
|
||||
@Override
|
||||
public BookCatalog transform(DataTable table) throws Throwable {
|
||||
|
||||
BookCatalog catalog = new BookCatalog();
|
||||
|
||||
table.cells()
|
||||
.stream()
|
||||
.skip(1) // Skip header row
|
||||
.map(fields -> new Book(fields.get(0), fields.get(1)))
|
||||
.forEach(catalog::addBook);
|
||||
|
||||
return catalog;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.cucumber.books;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import io.cucumber.java.Before;
|
||||
import io.cucumber.java.en.Given;
|
||||
import io.cucumber.java.en.Then;
|
||||
import io.cucumber.java.en.When;
|
||||
import io.cucumber.datatable.DataTable;
|
||||
|
||||
public class BookStoreRunSteps {
|
||||
|
||||
private BookStore store;
|
||||
private List<Book> foundBooks;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
store = new BookStore();
|
||||
foundBooks = new ArrayList<>();
|
||||
}
|
||||
|
||||
@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) {
|
||||
store.addBook(new Book(columns.get("title"), columns.get("author")));
|
||||
}
|
||||
}
|
||||
|
||||
@Given("^I have the following books in the store with transformer$")
|
||||
public void haveBooksInTheStoreByTransformer(BookCatalog catalog) {
|
||||
store.addAllBooks(catalog.getBooks());
|
||||
}
|
||||
|
||||
@When("^I search for books by author (.+)$")
|
||||
public void searchForBooksByAuthor(String author) {
|
||||
foundBooks = store.booksByAuthor(author);
|
||||
}
|
||||
|
||||
@Then("^I find (\\d+) books$")
|
||||
public void findBooks(int count) {
|
||||
assertEquals(count, foundBooks.size());
|
||||
}
|
||||
}
|
|
@ -2,8 +2,9 @@ package com.baeldung.shopping;
|
|||
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import cucumber.api.CucumberOptions;
|
||||
import cucumber.api.junit.Cucumber;
|
||||
import io.cucumber.junit.Cucumber;
|
||||
import io.cucumber.junit.CucumberOptions;
|
||||
|
||||
|
||||
@RunWith(Cucumber.class)
|
||||
@CucumberOptions(features = { "classpath:features/shopping.feature" })
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package com.baeldung.shopping;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import cucumber.api.java8.En;
|
||||
|
||||
import io.cucumber.java8.En;
|
||||
|
||||
public class ShoppingStepsDef implements En {
|
||||
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
Feature: Book Store
|
||||
|
||||
Scenario: Correct non-zero number of books found by author by list
|
||||
Given I have the following books in the store by list
|
||||
| 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: Correct non-zero number of books found by author by map
|
||||
Given I have the following books in the store by map
|
||||
| title | author |
|
||||
| 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: Correct non-zero number of books found by author with transformer
|
||||
Given I have the following books in the store with transformer
|
||||
| title | author |
|
||||
| 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
|
Loading…
Reference in New Issue