JBehave runner

JBehave is no exception to the rule that every type of test needs a runner. In the previous chapters, we used JUnit and TestNG runners. While neither of those needed any special configuration, JBehave is a bit more demanding and forces us to create a class that will hold all the configuration required for running stories.

The following is the Runner code that we'll use throughout this chapter:

public class Runner extends JUnitStories { 
 
  @Override 
  public Configuration configuration() { 
    return new MostUsefulConfiguration() 
                  .useStoryReporterBuilder(getReporter()) 
                  .useStoryLoader(new LoadFromURL()); 
  } 
 
  @Override 
  protected List<String> storyPaths() { 
    String path = "stories/**/*.story"; 
    return new StoryFinder().findPaths(
CodeLocations.codeLocationFromPath("").getFile(), Collections.singletonList(path), new ArrayList<String>(),
"file:"); } @Override public InjectableStepsFactory stepsFactory() { return new InstanceStepsFactory(configuration(), new Steps()); } private StoryReporterBuilder getReporter() { return new StoryReporterBuilder() .withPathResolver(new FilePrintStreamFactory.ResolveToSimpleName()) .withDefaultFormats() .withFormats(Format.CONSOLE, Format.HTML); } }

It is very uneventful code, so we'll comment only on a few important parts. The overridden method storyPaths has the location to our story files set to the stories/**/*.story path. This is a standard Apache Ant (http://ant.apache.org/) syntax that, when translated to plain language, means that any file ending with .story inside the stories directory or any subdirectory (**) will be included. Another important overridden method is stepsFactory, which is used to set classes containing the steps definition (we'll work with them very soon). In this case, we set it to the instance of a single class called Steps (the repository already contains an empty class that we'll use later on).

The source code can be found in the 01-runner branch of the tdd-java-ch08-books-store Git repository, at https://bitbucket.org/vfarcic/tdd-java-ch08-books-store/branch/01-runner.

Now that we have our runner done, it is time to fire it up and see what the result is.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.135.175