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 ...

Get The Cucumber for Java Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.