Home > jruby, maven, ruby, tech > Running cucumber on JRuby+Maven

Running cucumber on JRuby+Maven

Cucumber is an interesting offshoot of the popular BDD tool Rspec developed by Aslak Hellesoy. Cucumber executes plain-text feature specifications against code implementing those features and verifies it. Take a look at the cucumber examples to understand what this means in practice.

This post explains how you can configure it to write specifications for a Java project using Maven and JRuby.

1. Add JRuby as a dependency in your POM:

<project>
    ...
   <dependencies>
       <dependency>
           <groupId>org.jruby</groupId>
           <artifactId>jruby-complete</artifactId>
           <version>1.1.4</version>
       </dependency>
   </dependencies>
   ...
</project>

2. Configure the exec-maven-plugin to run cucumber during integration-test phase.

<project>
...
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
            <execution>
                <id>run-cucumber</id>
                <phase>integration-test</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>org.jruby.Main</mainClass>
                    <arguments>
                        <argument>-S</argument>
                        <argument>rake</argument>
                        <argument>features</argument>
                    </arguments>
                </configuration>
            </execution>
        </executions>
    </plugin>
...
</project>

3. Configure a profile to install cucumber if it is not already locally available:

<project>
...
    <profiles>
        <profile>
            <id>first.time</id>
            <activation>
                <file>
                    <missing>${user.home}/.jruby/lib/ruby/gems/1.8/gems/cucumber-0.1.8</missing>
                </file>
            </activation>
            
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.1</version>
                        <executions>
                            <execution>
                                <id>install-diff-lcs-gem</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                                <configuration>
                                    <mainClass>org.jruby.Main</mainClass>
                                    <arguments>
                                        <argument>-S</argument>
                                        <argument>gem</argument>
                                        <argument>install</argument>
                                        <argument>diff-lcs</argument>
                                        <argument>--no-ri</argument>
                                        <argument>--no-rdoc</argument>
                                        <argument>--no-test</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>install-hoe-gem</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                                <configuration>
                                    <mainClass>org.jruby.Main</mainClass>
                                    <arguments>
                                        <argument>-S</argument>
                                        <argument>gem</argument>
                                        <argument>install</argument>
                                        <argument>hoe</argument>
                                        <argument>--no-ri</argument>
                                        <argument>--no-rdoc</argument>
                                        <argument>--no-test</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>install-cucumber-gem</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                                <configuration>
                                    <mainClass>org.jruby.Main</mainClass>
                                    <arguments>
                                        <argument>-S</argument>
                                        <argument>gem</argument>
                                        <argument>install</argument>
                                        <argument>cucumber</argument>
                                        <argument>--no-ri</argument>
                                        <argument>--no-rdoc</argument>
                                        <argument>--no-test</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
...
</project>

4. Create a file named Rakefile as a sibling of your pom.xml, with the following contents:

require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features) do |t|
  t.cucumber_opts = "--format pretty"
end

5. Running

mvn integration-test

should run cucumber now on your project. Cucumber looks for feature specifications to run in the features directory.

Please see this maven project for a test project configured with cucumber.

About these ads
Categories: jruby, maven, ruby, tech
  1. November 18, 2008 at 1:03 pm

    Hi,

    Thanks for a great writeup!!

    I am linking to your post from the Cucumber docs

  2. c4milo
    January 28, 2009 at 4:25 am

    I have the following error:

    [INFO] [exec:java {execution: run-cucumber}]
    (in /home/camilo/Development/ceiba/java/medicina-prepagada)
    rake aborted!
    no such file to load — spec/rake/spectask
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’
    /home/camilo/Development/ceiba/java/medicina-prepagada/Rakefile:3
    /home/camilo/Development/ceiba/java/medicina-prepagada/Rakefile:2349:in `load’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2349:in `raw_load_rakefile’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1985:in `load_rakefile’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in `standard_exception_handling’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1984:in `load_rakefile’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1969:in `run’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in `standard_exception_handling’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1967:in `run’
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/bin/rake:31
    file:/home/camilo/.m2/repository/org/jruby/jruby-complete/1.1.6/jruby-complete-1.1.6.jar!/META-INF/jruby.home/lib/ruby/gems/1.8/gems/rake-0.8.3/bin/rake:19:in `load’
    /var/lib/gems/1.8/bin/rake:19

  3. March 25, 2009 at 10:13 pm

    I just released pure java support for Cucumber. You can read more about it here: http://www.nabble.com/-Cucumber–ANN%3A-Cucumber-with-pure-Java-td22704377.html

    Aslak

  4. Ole Christian Rynning
    April 2, 2009 at 12:18 pm

    And I added a basic cucumber-maven-plugin to that (now in http://github.com/aslakhellesoy/cucumber_java). :-)

  5. April 9, 2009 at 6:07 pm

    It seems like with this approach, jruby doesn’ t propagate the classpath to Cucumber. In the Rakefile:

    puts java.lang.System.getProperty(“java.class.path”) => MAVEN_HOME\bin\..\boot\classworlds-1.1.jar

    And instantiating classes from src/main/java works.

    In the steps-file:

    puts java.lang.System.getProperty(“java.class.path”) => USER_HOME\.jruby\bin\..\lib\jruby-complete-1.1.4.jar;.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

    (This is actually the contents of the CLASS_PATH environment variable)

    And instantiating classes from src/main/java does NOT work.

    Any idea as to how to get the class path into Cucumber?

  6. Aslak Hellesoy
    April 11, 2009 at 7:36 am

    Regarding questions about http://github.com/aslakhellesoy/cucumber_java/tree/master
    - please use the Cucumber/Rspec mailing list. Regarding the approach described in this blog – please help improve Cucumber-Java instead.

  1. December 15, 2008 at 2:18 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: