Skip to content

Commit fb06116

Browse files
authored
Merge pull request eugenp#8195 from albanoj2/BAEL-3444
BAEL-3444: Cucumber Data Table
2 parents 4355944 + 80e31fb commit fb06116

File tree

12 files changed

+245
-14
lines changed

12 files changed

+245
-14
lines changed

testing-modules/testing-libraries/pom.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
<version>${lambda-behave.version}</version>
1919
</dependency>
2020
<dependency>
21-
<groupId>info.cukes</groupId>
21+
<groupId>io.cucumber</groupId>
2222
<artifactId>cucumber-junit</artifactId>
2323
<version>${cucumber.version}</version>
2424
<scope>test</scope>
2525
</dependency>
2626
<dependency>
27-
<groupId>info.cukes</groupId>
27+
<groupId>io.cucumber</groupId>
2828
<artifactId>cucumber-java</artifactId>
2929
<version>${cucumber.version}</version>
3030
<scope>test</scope>
3131
</dependency>
32+
3233
<dependency>
33-
<groupId>info.cukes</groupId>
34+
<groupId>io.cucumber</groupId>
3435
<artifactId>cucumber-java8</artifactId>
3536
<version>${cucumber.version}</version>
3637
<scope>test</scope>
@@ -87,7 +88,7 @@
8788

8889
<properties>
8990
<lambda-behave.version>0.4</lambda-behave.version>
90-
<cucumber.version>1.2.5</cucumber.version>
91+
<cucumber.version>4.8.0</cucumber.version>
9192
<checkstyle-maven-plugin.version>3.0.0</checkstyle-maven-plugin.version>
9293
</properties>
9394

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.cucumber.books;
2+
3+
public class Book {
4+
5+
private String title;
6+
private String author;
7+
8+
public Book(String title, String author) {
9+
this.title = title;
10+
this.author = author;
11+
}
12+
13+
public Book() {}
14+
15+
public String getTitle() {
16+
return title;
17+
}
18+
19+
public void setTitle(String title) {
20+
this.title = title;
21+
}
22+
23+
public String getAuthor() {
24+
return author;
25+
}
26+
27+
public void setAuthor(String author) {
28+
this.author = author;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "Book [title=" + title + ", author=" + author + "]";
34+
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.cucumber.books;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class BookCatalog {
7+
8+
private List<Book> books = new ArrayList<>();
9+
10+
public void addBook(Book book) {
11+
books.add(book);
12+
}
13+
14+
public List<Book> getBooks() {
15+
return books;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return "BookCatalog [books=" + books + "]";
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.cucumber.books;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.Objects;
7+
import java.util.stream.Collectors;
8+
9+
public class BookStore {
10+
11+
private List<Book> books = new ArrayList<>();
12+
13+
public void addBook(Book book) {
14+
books.add(book);
15+
}
16+
17+
public void addAllBooks(Collection<Book> books) {
18+
this.books.addAll(books);
19+
}
20+
21+
public List<Book> booksByAuthor(String author) {
22+
return books.stream()
23+
.filter(book -> Objects.equals(author, book.getAuthor()))
24+
.collect(Collectors.toList());
25+
}
26+
}

testing-modules/testing-libraries/src/test/java/com/baeldung/calculator/CalculatorIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.baeldung.calculator;
22

3-
import cucumber.api.CucumberOptions;
4-
import cucumber.api.junit.Cucumber;
53
import org.junit.runner.RunWith;
64

5+
import io.cucumber.junit.Cucumber;
6+
import io.cucumber.junit.CucumberOptions;
7+
78
@RunWith(Cucumber.class)
89
@CucumberOptions(
910
features = {"classpath:features/calculator.feature", "classpath:features/calculator-scenario-outline.feature"}

testing-modules/testing-libraries/src/test/java/com/baeldung/calculator/CalculatorRunSteps.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.baeldung.calculator;
22

3-
import com.baeldung.cucumber.Calculator;
4-
import cucumber.api.java.Before;
5-
import cucumber.api.java.en.Given;
6-
import cucumber.api.java.en.Then;
7-
import cucumber.api.java.en.When;
83
import org.hamcrest.Matchers;
94
import org.junit.Assert;
105

6+
import com.baeldung.cucumber.Calculator;
7+
8+
import io.cucumber.java.Before;
9+
import io.cucumber.java.en.Given;
10+
import io.cucumber.java.en.Then;
11+
import io.cucumber.java.en.When;
12+
1113
public class CalculatorRunSteps {
1214

1315
private int total;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.cucumber.books;
2+
3+
import org.junit.runner.RunWith;
4+
5+
import io.cucumber.junit.Cucumber;
6+
import io.cucumber.junit.CucumberOptions;
7+
8+
@RunWith(Cucumber.class)
9+
@CucumberOptions(features = "classpath:features/book-store.feature")
10+
public class BookStoreIntegrationTest {
11+
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.cucumber.books;
2+
3+
import java.util.Locale;
4+
5+
import io.cucumber.core.api.TypeRegistry;
6+
import io.cucumber.core.api.TypeRegistryConfigurer;
7+
import io.cucumber.datatable.DataTable;
8+
import io.cucumber.datatable.DataTableType;
9+
import io.cucumber.datatable.TableTransformer;
10+
11+
public class BookStoreRegistryConfigurer implements TypeRegistryConfigurer {
12+
13+
@Override
14+
public Locale locale() {
15+
return Locale.ENGLISH;
16+
}
17+
18+
@Override
19+
public void configureTypeRegistry(TypeRegistry typeRegistry) {
20+
typeRegistry.defineDataTableType(
21+
new DataTableType(BookCatalog.class, new BookTableTransformer())
22+
);
23+
}
24+
25+
private static class BookTableTransformer implements TableTransformer<BookCatalog> {
26+
27+
@Override
28+
public BookCatalog transform(DataTable table) throws Throwable {
29+
30+
BookCatalog catalog = new BookCatalog();
31+
32+
table.cells()
33+
.stream()
34+
.skip(1) // Skip header row
35+
.map(fields -> new Book(fields.get(0), fields.get(1)))
36+
.forEach(catalog::addBook);
37+
38+
return catalog;
39+
}
40+
41+
}
42+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.cucumber.books;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import io.cucumber.java.Before;
10+
import io.cucumber.java.en.Given;
11+
import io.cucumber.java.en.Then;
12+
import io.cucumber.java.en.When;
13+
import io.cucumber.datatable.DataTable;
14+
15+
public class BookStoreRunSteps {
16+
17+
private BookStore store;
18+
private List<Book> foundBooks;
19+
20+
@Before
21+
public void setUp() {
22+
store = new BookStore();
23+
foundBooks = new ArrayList<>();
24+
}
25+
26+
@Given("^I have the following books in the store by list$")
27+
public void haveBooksInTheStoreByList(DataTable table) {
28+
29+
List<List<String>> rows = table.asLists(String.class);
30+
31+
for (List<String> columns: rows) {
32+
store.addBook(new Book(columns.get(0), columns.get(1)));
33+
}
34+
}
35+
36+
@Given("^I have the following books in the store by map$")
37+
public void haveBooksInTheStoreByMap(DataTable table) {
38+
39+
List<Map<String, String>> rows = table.asMaps(String.class, String.class);
40+
41+
for (Map<String, String> columns: rows) {
42+
store.addBook(new Book(columns.get("title"), columns.get("author")));
43+
}
44+
}
45+
46+
@Given("^I have the following books in the store with transformer$")
47+
public void haveBooksInTheStoreByTransformer(BookCatalog catalog) {
48+
store.addAllBooks(catalog.getBooks());
49+
}
50+
51+
@When("^I search for books by author (.+)$")
52+
public void searchForBooksByAuthor(String author) {
53+
foundBooks = store.booksByAuthor(author);
54+
}
55+
56+
@Then("^I find (\\d+) books$")
57+
public void findBooks(int count) {
58+
assertEquals(count, foundBooks.size());
59+
}
60+
}

testing-modules/testing-libraries/src/test/java/com/baeldung/shopping/ShoppingIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import org.junit.runner.RunWith;
44

5-
import cucumber.api.CucumberOptions;
6-
import cucumber.api.junit.Cucumber;
5+
import io.cucumber.junit.Cucumber;
6+
import io.cucumber.junit.CucumberOptions;
7+
78

89
@RunWith(Cucumber.class)
910
@CucumberOptions(features = { "classpath:features/shopping.feature" })

0 commit comments

Comments
 (0)