Writing Steps in JRuby

JRuby makes the Ruby language available to the JVM.

JRuby provides the extensions to Ruby that are required by JBehave to allow writing Steps classes directly in Ruby: method annotations and signatures.

The JRuby steps classes must provide method metadata specifying the java annotation and signature:

require 'java'
 
java_package 'org.jbehave.examples.jruby'
 
class JRubySteps
 
   java_annotation 'org.jbehave.core.annotations.Given("a date of $date")'
   java_signature 'void givenDate(java.util.Date)'
   def date(date)
     org.junit.Assert.assertNotNull(date)
   end
 
  java_annotation 'org.jbehave.core.annotations.When("$days days pass")'
  java_signature 'void whenDaysPass(int)'
  def daysPass(days)
     org.junit.Assert.assertNotNull(days)
  end
 
  java_annotation 'org.jbehave.core.annotations.Then("the date is $date")'
  java_signature 'void thenTheDate(java.util.Date)'
  def theDate(date)
     org.junit.Assert.assertNotNull(date)
  end
 
end
The JRuby classes must be compiled into Java bytecode.

The jrubyc executable supports the --javac option to generate Java sources:

jrubyc --javac -c ${jruby.classpath} --target ${jruby.generated.sources} src/main/rb/*.rb

The jruby-complete artifact contains the jrubyc script in the META-INF/jruby.home/bin directory.

If using Maven, the jruby-maven-plugin can do the same task more cleanly:

<plugin>
  <groupId>de.saumya.mojo</groupId>
  <artifactId>jruby-maven-plugin</artifactId>
  <version>0.29.1</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration>
        <generateJava>true</generateJava>
        <generatedJavaDirectory>${jruby.generated.sources}</generatedJavaDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>