Hints for getting JBehave2 up and running

I started fiddling a bit with the new JBehave2 yesterday. It looks quite a bit like RSpec for Java, which sounds great on paper. Since I have a long history with Fitnesse I would be interested to try another approach.

To get started with JBehave, me and my pair programming partner Måns decided to try and get the example from the JBehave home page to work. We had 30 minutes to do it. Three classes and a one text file. We should be able to do it! We failed (of course).

Back home I took a second look at the problem and managed to get a first example working (not exactly the one from the JBehave home page, but more of a Hello World example). It was a bit tricky though so I thought I would share some experiences.

This my (pathetic) little scenario:

Given I have a greeting system
When I give my name as Måns
Then I should see a greeting, “Hello, Måns!”

Here are some hints to get your first example working:

  • Don’t put your code in the default package. We did this. But putting your code into the default package leads to spurious NullPointerException on line 36 of the UnderscoredCamelCaseResolver. (This line looks a little train wreckish, which is ironic in a BDD package.)

String packageDir = scenarioClass.getPackage().getName().replaceAll(DOT_REGEX, SLASH);

  • Quotes in the test definition must be escaped by a backslash in the Java annotation to actually work.  In the sample scenario text file a message is surrounded by double quotes. The sample code on the start page will not work but \”$message\” will.
  • It is mentioned on the JBehave home page, but worth repeating, that some versions of Eclipse (my version of Ganymede at least) cannot run the JBehave scenario out of the box. The reason for this is apperently that the JUnit runner cannot find the @Test annotation inside the JBehave core jar. (This sounds like a bug in Eclipse to me.) To fix it now you need to download and attach the source code for JBehave core to the JAR file. This is done in the project properties Java Build Path editor. Open the jar and indicate to Eclipse the location of the source archive. Now Eclipse should see the @Test annotation and you can run the scenario.
  • The example code mentions a “matcher” method called containsMessage. Matchers are used to create specifications and assertions. This is probably a typo, why would there be a general matcher method with that name? Or maybe I have misunderstood? However, the Hamcrest matchers do contain a matcher method called containsString, which sounds a bit more plausible. Change the name.
  • To get the containsString matcher working I had to download the hamcrest-all-1.1.jar and put it on the class path. I believe that the Hamcrest core matchers are part of JUnit 4.4+ but the containsString matcher is part of the hamcrest text matchers. Make a static import of org.hamcrest.Matchers.* to access all matchers.

For non-english speakers you will be happy to know that characters outside the ASCII set works. Now I need to find a way to translate the keywords “Given”, “When” and “Then” to Swedish to get a full Swedish example working. Good luck!

2 thoughts on “Hints for getting JBehave2 up and running

  1. Interesting stuff. Sounds like it still has a few rough edges though. Of course I am glad (in a selfish way) that you wrote this in English so I can actually read it…

Comments are closed.