Testing / Cucumber Interview Questions
How does Cucumber handle transforming Data Tables into Java objects?
Beyond the built-in conversions (asMaps(), asLists(), asMap() for two-column tables), Cucumber-JVM lets a project register custom transformers so a Data Table converts directly into a list of domain objects, avoiding repetitive manual field-by-field parsing inside every step definition that consumes tabular data.
public class User { public String username; public String role; public boolean active; } public class Transforms { @DataTableType public User userEntry(Map<String, String> entry) { User user = new User(); user.username = entry.get("username"); user.role = entry.get("role"); user.active = Boolean.parseBoolean(entry.get("active")); return user; } } @Given("the following users exist:") public void the_following_users_exist(List<User> users) { users.forEach(userRepository::create); }
Once a @DataTableType transformer is registered for a given class, any step definition parameter typed as List<User> (matching a Data Table with the right column headers) automatically receives fully constructed User objects, with the conversion logic centralized in one place rather than duplicated across every step definition that happens to consume user data.
More Related questions...