JBehave 2 is up on its usual svn

svn co http://svn.codehaus.org/jbehave/trunk

No ‘official releases’ yet, but if you want to play, go ahead. Examples of examples are checked into trunk.

Features include:

  • plain text scenarios
  • steps defined using annotations that match the scenarios
  • built on JUnit
  • Ensure (which is just an extension of Assert, so you don’t have to use it)
  • auto-conversion of string arguments to numbers
  • pending steps generated for anything which hasn’t been defined yet (these don’t break the build)
  • errors from scenarios are rethrown after the full scenario has been described

Our goal with JBehave 2 was to make a framework that was lightweight, easy to use and didn’t take as long to set up as JBehave 1. To that end, we’ve been driving this entirely from example code – no guessing, unless it’s obvious – and we’ve ended up with something that works quite a lot like RSpec’s story runner, but for Java.

  • To use it, simply name your scenario file with underscores, eg: i_can_toggle_a_cell and define steps in it thus:
    Given a 5 by 5 game
    When I toggle the cell at (2, 3)
    Then the grid should look like
    .....
    .....
    .....
    ..X..
    .....
    When I toggle the cell at (2, 4)
    Then the grid should look like
    .....
    .....
    .....
    ..X..
    ..X..
    When I toggle the cell at (2, 3)
    Then the grid should look like
    .....
    .....
    .....
    .....
    ..X..
  • Then extend the Scenario class with a similarly named Java class:
    ICanToggleACell.java
  • Extend the Steps class – this is where your steps will be defined, and you can have as many as you want
  • Inject these into the Scenario:
    public class ICanToggleACell extends Scenario {
    
    	public ICanToggleACell() {
    		super(new GridSteps()); // varargs, can have lots of steps
    	}
    }
  • Annotate methods in your step class to match the ones in your plain text scenario.
    public class GridSteps extends Steps {
    
    	private Game game;
    	private StringRenderer renderer;
    
    	@Given("a $width by $height game")
    	public void theGameIsRunning(int width, int height) {
    		game = new Game(width, height);
    		renderer = new StringRenderer();
    		game.setObserver(renderer);
    	}
    
    	@When("I toggle the cell at ($ column, $row)")
    	public void iToggleTheCellAt(int column, int row) {
    		game.toggleCellAt(column, row);
    	}
    
    	@Then("the grid should look like $grid")
    	public void theGridShouldLookLike(String grid) {
    		ensureThat(renderer.asString(), equalTo(grid));
    	}
    
    }
  • Run your new scenario!

Future features we’re thinking of:

  • having more than one scenario in a file
  • tagging scenarios
  • better tolerance of whitespace
  • an option for pending steps to break the build
  • decent documentation to show you how you can customise filename parsing, argument parsing, etc.
  • anything you persuade us you need.

Give it a go and tell us what you think! Release 2.0 can’t be far away…

This entry was posted in jbehave and tagged , . Bookmark the permalink.