-
Notifications
You must be signed in to change notification settings - Fork 72
Java
The cuke4duke jar contains extensions that allow you to define your Cucumber Step Definitions in pure Java instead of Ruby. So instead of doing this:
Given /I have (\d+) cukes in my belly/ do |n| @belly ||= [] n.to_i.times {|i| @belly << "cuke"} end
You can implement step definitions in Java:
package cukes; import cuke4duke.Given; import cuke4duke.Steps; import java.util.List; import java.util.ArrayList; @Steps public class BellySteps { private List<String> belly = new ArrayList<String>(); @Given("I have (\\d+) cukes in my belly") public void bellyCukes(int cukes) { for(int i = 0; i < cukes; i++) { belly.add("cukes"); } } }
You also need to tell Cuke4Duke what Step Definitions to load. This is done with cucumber
’s familiar --require
command line argument – except that you have to point it to the directory containing the compiled *.class
files of your step definitions.
While Java is admittedly more verbose than Ruby, there are a couple of benefits to using Java Step Definitions over pure Ruby ones:
- You can use your IDE’s standard tools like refactoring and code completion.
- Nobody on the team has to learn Ruby.
(Some people consider these points weaknesses, but that’s a philosophical discussion that won’t be covered here).
Before Cucumber runs a scenario it will use PicoContainer or Spring to create an instance of every class that uses Cuke4Duke annotations.
For each step in the current scenario, Cucumber will look for methods with Cuke4Duke annotations and a matching regexp, and if it finds one it will execute it. Cuke4Duke takes care of converting parameters to the method’s signature types.