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...

    <!-- jrubyHome points to the JRuby installation you wish to use (usually ${env.JRUBY_HOME}) -->
    <!-- sourceDirectory references where your RSpec tests reside -->
    <!-- outputDirectory specifies where the RSpec report should be placed -->

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

describe WarRootHandler do

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

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

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

  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