Java Respects RSpec
Bob McWhirter
18 April 2009

After living la vida Ruby for a while, I've grown to love RSpec.  It's truly quite flexible, and seems much quicker-to-the-point than JUnit for testing things.

Type-safety is nice and all, but when I'm writing tests, duck-typing is fantastic.

Someone previously had authored the rspec-maven-plugin.  But it seemed to sit idle, so I've usurped ownership lately.  The plugin, if you don't know, allows you to run RSpec, via JRuby, against your Java code, during the typical mvn test lifecycle phase.

Since I'm doing testing against some hard-core app-server innards, I started tripping over some shortcomings of the plugin.  In the process, I've managed to slip another language into the mix.

First, my code deals with JBoss-VFS, which itself mucks about with URL protocol handles to enable nice things like vfsmemory:// URLs and such.  But installing these handlers has some fairly strict classloading requirements, namely that they need to be in the system classloader. 

So now rspec-maven-plugin forks and sets up your dependencies in the root classloader.

The output during a test-run was sub-par, requiring you to open the HTML test report to really find out how things did.

Now I've added in a console-based progress formatter akin to the JUnit output

[INFO] Running RSpec tests from /Users/bob/oddthesis/jboss-rails/src/test/specs
SPEC: deployers/app_rails_yaml_parsing_deployer_spec.rb
- Java::OrgJbossRailsCoreDeployers::AppRailsYamlParsingDeployer
1 passing; 0 failing; 0 pending
SPEC: deployers/crypto_yaml_parsing_deployer_spec.rb
- Java::OrgJbossRubyEnterpriseCryptoDeployers::CryptoYamlParsingDeployer
1 passing; 0 failing; 0 pending
SPEC: deployers/rails_env_yaml_parsing_deployer_spec.rb
- Java::OrgJbossRailsCoreDeployers::RailsEnvYamlParsingDeployer
1 passing; 0 failing; 0 pending
SPEC: deployers/rails_structure_spec.rb
- Java::OrgJbossRailsCoreDeployers::RailsStructure
0 passing; 0 failing; 1 pending
SPEC: utils/string_utils_spec.rb
- Java::OrgJbossRubyCoreUtil::StringUtils camelize
3 passing; 0 failing; 0 pending
- Java::OrgJbossRubyCoreUtil::StringUtils converting paths to class names
3 passing; 0 failing; 0 pending
- Java::OrgJbossRubyCoreUtil::StringUtils converting paths to class names accomodating namespaces
2 passing; 0 failing; 0 pending
=========================================
TOTAL: 11 passing; 0 failing; 1 pending

You still get the HTML report, of course.

Also, since you like RSpec, you probably dislike Maven grinding away, searching the internet for non-existent POMs, and generally being a time-waster.  (If you say "Nexus" I'll punch you in the neck.)

So, after you've run mvn test once, you'll have run-rspecs.sh sitting in your target/ directory.  (There's the 3rd language, plain ol' shell).  This simply re-creates the fork() and CLASSPATH manipulation before invoking jruby to execute the actual script.

If you want to re-run your tests, quickly, just execute that script:

$ ./target/run-rspecs.sh

If you're futzing around with just a subset of tests, you can pass any normal spec command-line option to it.

This invocation will run only the specs under my test/specs/deployers/ directory:

$ ./target/run-rspecs.sh -p deployers/**_spec.rb

Later, I'll describe how I'm actually writing my RSpec tests to give my JBoss Microcontainer deployers a good thrashing.

To use this, you'll want the latest SNAPSHOT from the Codehaus snapshot repository.

 

  • Next An introduction to JBoss Cloud
  • Previous JBoss-Cloud 1.0.0.Beta4 release for holidays
Copyright 2008-2010 Red Hat, Inc.