Skip to content

Commit cebeda9

Browse files
authored
BAEL-8447: Handling Empty String With Cucumber Data Table (#19099)
1 parent 0bb7f2b commit cebeda9

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

testing-modules/testing-libraries/src/test/java/com/baeldung/cucumber/books/BookStoreRegistryConfigurer.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,30 @@ public void configureTypeRegistry(TypeRegistry typeRegistry) {
2121
new DataTableType(BookCatalog.class, new BookTableTransformer())
2222
);
2323
}
24-
24+
2525
private static class BookTableTransformer implements TableTransformer<BookCatalog> {
2626

2727
@Override
2828
public BookCatalog transform(DataTable table) throws Throwable {
29-
3029
BookCatalog catalog = new BookCatalog();
31-
30+
3231
table.cells()
3332
.stream()
3433
.skip(1) // Skip header row
35-
.map(fields -> new Book(fields.get(0), fields.get(1)))
34+
.map(fields -> {
35+
String title = fields.get(0);
36+
String author = fields.get(1);
37+
38+
// Handle empty strings
39+
if (author != null && author.isEmpty()) {
40+
author = "Unknown Author"; // Apply default value
41+
}
42+
43+
return new Book(title, author);
44+
})
3645
.forEach(catalog::addBook);
37-
46+
3847
return catalog;
3948
}
40-
4149
}
42-
}
50+
}

testing-modules/testing-libraries/src/test/java/com/baeldung/cucumber/books/BookStoreRunSteps.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public void searchForBooksByAuthor(String author) {
5656
foundBooks = store.booksByAuthor(author);
5757
}
5858

59+
@When("^I search for books by unknown authors$")
60+
public void searchForBooksByUnknownAuthors() {
61+
foundBooks = store.booksByAuthor("Unknown Author");
62+
}
63+
5964
@Then("^I find (\\d+) books$")
6065
public void findBooks(int count) {
6166
assertEquals(count, foundBooks.size());

testing-modules/testing-libraries/src/test/resources/features/book-store.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,15 @@ Feature: Book Store
2424
| The Lion, the Witch and the Wardrobe | C.S. Lewis |
2525
| In the Garden of Beasts | Erik Larson |
2626
When I search for books by author Erik Larson
27+
Then I find 2 books
28+
29+
Scenario: Finding books with some missing author data
30+
Given I have the following books in the store with transformer
31+
| title | author |
32+
| The Devil in the White City | Erik Larson |
33+
| The Lion, the Witch and the Wardrobe | C.S. Lewis |
34+
| In the Garden of Beasts | Erik Larson |
35+
| Untitled Manuscript | | # Empty author cell
36+
| Anonymous Work | | # Empty author cell
37+
When I search for books by unknown authors
2738
Then I find 2 books

0 commit comments

Comments
 (0)