Archive

Archive for the ‘jruby’ Category

Running cucumber on JRuby+Maven

October 29, 2008 8 comments

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.

Categories: jruby, maven, ruby, tech