Testing / Cucumber Interview Questions
What is a Data Table in Cucumber?
A Data Table is a pipe-delimited table attached directly beneath a Gherkin step, letting that single step carry structured, tabular data as an argument, rather than needing one step per row of data.
Given the following users exist: | username | role | active | | alice | admin | true | | bob | user | true | | carol | user | false |
@Given("the following users exist:") public void the_following_users_exist(DataTable dataTable) { List<Map<String, String>> rows = dataTable.asMaps(); for (Map<String, String> row : rows) { userRepository.create(row.get("username"), row.get("role"), Boolean.parseBoolean(row.get("active"))); } }
Cucumber's DataTable type offers several built-in conversions (a list of maps, a list of lists, a single map for two-column tables) and also supports registering custom transformers to convert each row directly into a domain object, which keeps the step definition code from having to manually parse raw strings for every field.
More Related questions...