Create a textual scenario file with a name that expresses the behaviour to verify, e.g. i_can_toggle_a_cell and define steps in it:
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..
Steps must start with one of the keywords highlighted (see Concepts for more details) and are not limited to a single line.
Define your Steps class, e.g. call it GridSteps, which will contain the Java methods that are mapped to the textual steps. The methods need to annotated with one of the JBehave annotations and the annotated value should contain a regex pattern that matches the textual step:
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) { Ensure.ensureThat(renderer.asString(), CoreMatchers.equalTo(grid)); } }
Define your Scenario class with a name that can be mapped to the textual scenario filename, e.g. ICanToggleACell.java:
public class ICanToggleACell extends Scenario { public ICanToggleACell() { super(new GridSteps()); // varargs, can have lots of steps } }
The Scenario is now configured to use the GridSteps that hold the step mappings.
Open your favourite IDE, the ICanToggleACell.java class will allow itself to run as a JUnit test. Be sure to check that you have all the required dependencies in your classpath.