Skip to content
Harry Cummings edited this page Jul 13, 2014 · 14 revisions

Tests are a very important communication tool and I wanted a test framework that would allow me to express myself as clearly as possible. I found the RSpec style of testing (which I came to know through the JavaScript test frameworks Jasmine and Mocha) to be particularly flexible and expressive.

Two key features of RSpec's syntax make it possible to write much more descriptive tests with richer contextual information:

  • Test names are strings and don't have to be valid Java method names
  • Tests can have a nested structure rather than being a flat list of methods I wanted to have the same benefits when writing tests on Java projects.

Scope

The intent is to keep this library very small. It relies on JUnit for execution and reporting/tooling, and does not attempt to provide its own solution for assertions or test doubles. Users of JarSpec are free to pick whichever libraries they prefer, using standard JUnit or Hamcrest assertions and matchers (which are included by default as a transitive dependency), and/or pulling in whatever other libraries they prefer to use (such as Mockito for test doubles).

Alternatives

There are frameworks for other JVM languages that support this kind of syntax. RSpec itself of course could be used on the JVM thanks to JRuby, and ScalaTest also supports this through its FunSpec class. I don't like to introduce extra languages to a project without good reason though, and I thought Java 8 should be up to the task on its own.

It is possible to write nested test classes with JUnit, for example using junit-nested or the experimental Enclosed runner.

The Gherkin language obviously places human-readability first, and there is a Java implementation of Cucumber. However, I think the use case for these is more business-facing tests and I wouldn't want to use them for my day-to-day unit tests, which should server developers first (both as the people who have to write them and as the audience).

Finally, both Jnario (based on Xtend) and Spock (based on Groovy) offer their own variations on the various BDD-style syntax options mentioned above. While these frameworks definitely allow for more expressive tests, I thought (perhaps unfairly) that Jnario's integration with Eclipse limited developer tooling options, and Spock's syntax didn't provide the same benefits as those mentioned for RSpec above.