O'Reilly logo

The Cucumber for Java Book by Seb Rose, Matt Wynne, Aslak Hellesoy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Capturing Arguments

You’ll notice that in the step we’ve been using as an example, we’ve talked about the sum of $100 the whole time. What if we had another scenario where we needed to deposit a different amount of money into the account? Would we need another step definition, like this?

 
@Given(​"I have deposited \\$100 in my Account"​)
 
public​ ​void​ iHaveDeposited$100InMyAccount() {
 
// TODO: code goes here
 
}
 
 
@Given(​"I have deposited \\$250 in my Account"​)
 
public​ ​void​ iHaveDeposited$250InMyAccount() {
 
// TODO: code goes here
 
}

Happily, we don’t. This is where the flexibility of regular expressions comes into play. We can use two of regular expressions’ most useful features here to capture any dollar amount as an argument ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required