JBehave is designed to be embedded in different development environments. The JBehave Core module contains support for running stories as JUnit tests - which can be run either in your favourite IDE or in your command-line build that supports JUnit tests. Other unit testing frameworks, e.g. TestNG or Spring Test, can also be used very easily, c.f. FAQ.
You can also run stories using the JBehave Maven goals, which provide a facade around the Embedder functionality.
JBehave provides the Embedder as the core entry point to running stories. The Embedder can be configured in multiple way, c.f. the configuration.
Stories can be run in different modes:
The Embedder can also be used to run the stories in any IDE environment, without the need to extend any base class. In the following example we use JUnit annotatation to denote different running instances:
public class TraderStoryRunner { @Test public void runClasspathLoadedStoriesAsJUnit() { // Embedder defines the configuration and candidate steps Embedder embedder = new TraderEmbedder(); List<String> storyPaths = ... // use StoryFinder to look up paths embedder.runStoriesAsPaths(storyPaths); } @Test public void runURLLoadedStoriesAsJUnit() { // Embedder defines the configuration and candidate steps Embedder embedder = new URLTraderEmbedder(); List<String> storyPaths = ... // use StoryFinder to look up paths embedder.runStoriesAsPaths(storyPaths); } } |
where the TraderEmbedder/URLTraderEmbedder define the configuration using the loading from the classpath and URL resources, respectively. E.g.:
public class TraderEmbedder extends Embedder { @Override public EmbedderControls embedderControls() { return new EmbedderControls().doIgnoreFailureInStories( true ).doIgnoreFailureInView( true ); } @Override public Configuration configuration() { Class<? extends TraderEmbedder> embedderClass = this .getClass(); return new MostUsefulConfiguration() .useStoryLoader( new LoadFromClasspath(embedderClass.getClassLoader())) .useStoryReporterBuilder( new StoryReporterBuilder() .withCodeLocation(CodeLocations.codeLocationFromClass(embedderClass)) .withDefaultFormats() .withFormats(CONSOLE, TXT, HTML, XML)) .useParameterConverters( new ParameterConverters() .addConverters( new DateConverter( new SimpleDateFormat( "yyyy-MM-dd" )))) // use custom date pattern .useStepPatternParser( new RegexPrefixCapturingPatternParser( "%" )) // use '%' instead of '$' to identify parameters .useStepMonitor( new SilentStepMonitor()); } @Override public InjectableStepsFactory stepsFactory() { return new InstanceStepsFactory(configuration(), new TraderSteps( new TradingService()), new BeforeAfterSteps()); } } |
JBehave supports running both local and remote stories. To run remote stories, we need to use a URL-based loader with an appropriate remote code location. The difference w.r.t. the local run is minimal:
public class RemoteTraderStories extends TraderStories { @Override public Configuration configuration() { return super .configuration() .useStoryLoader( new LoadFromURL()) .useStoryReporterBuilder( new StoryReporterBuilder() .withDefaultFormats() .withFormats(CONSOLE, TXT, HTML, XML)); } @Override protected List<String> storyPaths() { // Specify story paths as remote URLs .toExternalForm(); return asList(codeLocation + "and_step.story" ); } } |
By default, the story runners are configured to fail-fast, i.e. the execution will stop at first failed story (but will complete execution of the all the scenarios in the story first). To allow the generation of a complete stories view (reporting how many stories failed), the runners need to be enabled to run stories with ignoreFailureInStories flag set to true. In this way, all stories will run and the failure will be assessed only during the view generation. If any stories failed, the build will fail correspondingly. Should the need to ignore failure in the view arise (although generally not recommended), one can set the ignoreFailureInView flag to true.