Create a textual story file with a name that expresses the behaviour to verify, e.g. i_can_toggle_a_cell.story 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. Also, unless otherwise indicated, a story has at least one implicit scenario, each of which is a collection of steps.
Define your GridSteps class, a simple POJO, 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 { // Look, Ma', I'm a POJO! private Game game; private StringRenderer renderer; @Given ( "a $width by $height game" ) @Aliases (values={ "a new game: $width by $height" }) 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 toggleTheCellAt( int column, int row) { game.toggleCellAt(column, row); } @Then ( "the grid should look like $grid" ) @Aliases (values={ "the grid should be $grid" }) public void theGridShouldLookLike(String grid) { assertThat(renderer.asString(), equalTo(grid)); } } |
Define your Embeddable class which provides the link between the JBehave's executor framework (called Embedder) and the textual stories. The simplest configuration is a one-to-one mapping between a Java class and a textual story file. So we'll extend JUnitStory and give it a name that can be (conventionally) mapped to the textual story filename, e.g. ICanToggleACell.java:
@RunWith (JUnitReportingRunner. class ) public class ICanToggleACell extends JUnitStory { // Here we specify the configuration, starting from default MostUsefulConfiguration, and changing only what is needed @Override public Configuration configuration() { return new MostUsefulConfiguration() // where to find the stories .useStoryLoader( new LoadFromClasspath( this .getClass())) // CONSOLE and TXT reporting .useStoryReporterBuilder( new StoryReporterBuilder().withDefaultFormats().withFormats(Format.CONSOLE, Format.TXT)); } // Here we specify the steps classes @Override public InjectableStepsFactory stepsFactory() { // varargs, can have more that one steps classes return new InstanceStepsFactory(configuration(), new GridSteps()); } } |
The story is now configured to use the GridSteps that defines mappings between the textual steps and the Java methods to be executed.
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.
Since we've defined two reports, CONSOLE and TXT, you should see during the running of the story the output being written the System.out. In addition, the same output will also have been written to a file in target/jbehave.