-
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.annotation.I18n.EN.Given; import java.util.List; import java.util.ArrayList; 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("cuke " + i); } } }
Cuke4Duke takes care of converting parameters to the method’s signature types.
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).
But hey, maybe you should try Groovy instead?
Before Cucumber runs a scenario it will use PicoContainer, Spring or Guice to create an instance of every class that uses Cuke4Duke annotations on methods. For example, if your preferred language is English:
cuke4duke.annotation.I18n.EN.Given
cuke4duke.annotation.I18n.EN.Then
cuke4duke.annotation.I18n.EN.When
You can read more about Step Definitions in the Cucumber wiki.
If you want to control how methods are reported in the output, see Method Format.