Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.
aslakhellesoy edited this page Sep 13, 2010 · 11 revisions

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 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("cukes");
        }
    }
}

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?

How it works

Before Cucumber runs a scenario it will use PicoContainer or Spring to create an instance of every class that uses Cuke4Duke annotations on methods:

  • cuke4duke.Given
  • cuke4duke.When
  • cuke4duke.Then

You can read more about Step Definitions in the Cucumber wiki.

Clone this wiki locally