Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Transform

kolach edited this page Dec 10, 2010 · 2 revisions

When a step matches a step definition with groups in the Regexp, those group values are passed to the step definition. If a step definition declares arguments of a primitive type such as int or double, cuke4duke will do the conversion transparently.

Sometimes it can be handy to transform a string to a more high level, domain specific object. You can achieve this with a custom transform:

Java

Just annotate a method with @Transform:

@Transform
public User transformStringToUserWithAge(String age) {
    return new User(Integer.valueOf(age));
}

Whenever Cuke4Duke is about to invoke a step definition with arguments different than String, it will look among all registered @Transform methods and invoke it if it finds one. (If it finds 0, 2 or more of the required type it will fail).

@Given("^I pass '(.*)' to a method with User as parameter$")
public void transformToA(User user) {
    this.user = user;
}

Scala

implicit val t2User = Transform[User](name => User.find_by_username(name))

Then("""^(\w+) should be friends with (\w+)$"""){ (user:User, friend:User) =>
    assertTrue(user.friends_with(friend))
}
Clone this wiki locally