public
Description: BDD that talks to domain experts first and code second
Home | Edit | New

JRuby and Java

Cucumber runs fine on JRuby. This means you can use Cucumber with plain Java code.

Installing Cucumber in JRuby

To get started, you need the JDK, JRuby and Cucumber installed. I’m assuming you already know how to install a JDK – you probably already have it installed since you’re reading this page.

Install JRuby

Just download the latest binary distribution, unpack it and make sure the bin directory is on your PATH. Run the following command to update RubyGems:

jruby -S gem update --system
To check that you have installed it properly, try out the following commands:
jruby --version

and

jruby -S rake --version

Install Cucumber into JRuby

Just use the gem command from JRuby:

jruby -S gem install cucumber

To check that you have it installed properly, run:

jruby -S cucumber --help

When you run jruby with the -S switch, jruby will look for the script (in this case cucumber) in jruby’s bin directory before it starts looking at the PATH. It will find the cucumber ruby script that was created when you installed the gem inside JRuby’s bin directory.

Running the examples

The examples are included in the gem.

Open a command prompt and cd to the Cucumber examples directory. You’ll find them under $JRUBY_HOME/lib/ruby/gems/1.8/gems/cucumber-X.Y.Z/examples. (Replace X.Y.Z with the Cucumber version you installed above).

If you pulled the Cucumber code with Git, you’ll find the examples under examples.

Run the plain ruby example

cd i18n/en
jruby -S cucumber features

The Code under test is pure Ruby, so it’s not that interesting in the context of JRuby.

Run the Java example

cd java
jruby -S rake features

This will compile the java code and then run Cucumber features against that code.

Using Maven

Apache Maven is a popular build tool for Java. If you’re already using Maven you might be interested in running Cucumber with Maven too. Check out the cuke4duke project, which has a Maven plugin that lets you run Cucumber from Maven.

(An older approach is described in Madaspeak’s great blog post).

Using Ant

Here is an example build file:

<project name="Cucumber Demo" default="cucumber" basedir=".">
  <target name ="compile" description="Compile classes">
    <mkdir dir="build" />
    <javac srcdir="src" destdir="build" />
  </target>

  <target name="cucumber" depends="compile" description="Run Cucumber">
    <property environment="ENV" />
    <java classname="org.jruby.Main" fork="true" failonerror="true">
      <classpath>
        <pathelement path="${ENV.JRUBY_HOME}/lib/jruby.jar"/>
        <pathelement path="build"/>
      </classpath>
      <jvmarg value="-Djruby.home=${ENV.JRUBY_HOME}"/>
      <arg value="-S"/>
      <arg value="cucumber"/>
      <arg value="--format"/>
      <arg value="pretty"/>
      <arg value="--format"/>
      <arg value="junit"/>
      <arg value="--out"/>
      <arg value="build"/>
      <arg value="features"/>
    </java>
  </target>
</project>

You can find the full example under

examples/java
in the Cucumber source code

Seen elsewhere

Please add link to examples using Cucumber and JRuby or Java here:

Last edited by demetriusnunes, Tue Oct 27 07:43:46 -0700 2009
Home | Edit | New
Versions: