Maven, Java and RSpec

18 September 2008

java junit maven rspec testing

Since I've been back on the job, writing Java code lately, that means I've been testing Java code lately.

After living in the land of Ruby with RSpec, thinking about JUnit did not excite me. Thankfully I found the rspec-maven-plugin, which integrates straight into the maven test process.

I like rspec because it removes a whole lot of the cruft involved in writing tests. When I return to the specs weeks later, I can still read the intent. So often, with JUnit tests named with cryptic method names, it's difficult to ascertain exactly what's intended to be tested.

The beta-4 plugin on the repository is based on jruby-1.1.2, but in SVN I've updated it to support 1.1.4. I think there's still some more improvements to be made, such as respecting maven.test.skip and getting rid of the need for JRUBY_HOME pointing to a full installation. (Note: I did not write this plugin, I've just started contributing to it).

After a little configuration of the plugin in your pom.xml...

  
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>rspec-maven-plugin</artifactId>
  <configuration>
    <!-- jrubyHome points to the JRuby installation you wish to use (usually ${env.JRUBY_HOME}) -->
    <jrubyHome>${env.JRUBY_HOME}</jrubyHome>
    <!-- sourceDirectory references where your RSpec tests reside -->
    <sourceDirectory>${basedir}/src/test/specs</sourceDirectory>
    <!-- outputDirectory specifies where the RSpec report should be placed -->
    <outputDirectory>${basedir}/target</outputDirectory>
    <!--<skipTests>true</skipTests>-->
  </configuration>
  <executions>
    <execution>
      <id>test</id>
      <phase>test</phase>
      <goals>
        <goal>spec</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Now, just start flinging out specs in ./src/test/specs/.

This is a portion of a test against a JBoss-VFS VirtualFileHandler implementation, written in Java in WarRootHandler.java:

  
describe WarRootHandler do

  before(:each) do
    @context = RailsAppContextFactory.getInstance.createRoot( "ballast", File.dirname( __FILE__ ) + '/../ballast' )
    @root = @context.get_war_root_handler
  end

  it "should have a rails:// URL" do
    @root.to_uri.to_string.should eql( "rails://ballast/" )
  end

  it "should be resolvable through java URL handlers" do
    url = java.net.URL.new( "rails://ballast/" )
    url.to_s.should_not be_nil
  end

  it "should delegate for WEB-INF requests" do
    web_inf = @root.get_child( 'WEB-INF' )
    web_inf.should_not be_nil
    jboss_rails_yml = web_inf.get_child( 'jboss-rails.yml' )
    jboss_rails_yml.should_not be_nil
  end
end