6.7.16

JBehave Configuration for BDD


Custom Configurations for JBehave

1.   Meta Filters -

·      Meta filtering is most useful when we want to perform selective running of stories/scenarios.
To enable filtering in JBehave based on Meta tags, just add the following 3 lines in the MyStories class, under the Constructor [MyStories() - created as part of the simple archetype]

List<String> metaFilters = new ArrayList<String>();
metaFilters.add(System.getProperty("metaFilters", "-skip"));
configuredEmbedder().useMetaFilters(metaFilters);

2.   Avoid @Named annotation –

·      This makes jbehave interpret the name contained between the delimiters as the parameter name and look it up in the parameters provided by the Examples table. The default behaviour of parameter lookup is overridden via the ParameterControls.
·      This helps to avoid the use of @Named parameter annotation in the method definitions, and also avoids different aliases of steps
Add this line in the end of configuration() method –

.useParameterControls(new ParameterControls().useDelimiterNamedParameters(true)
    
                             
3.   Change the delimiters to [] –

·      This would define the delimiters to be used for parameter names. This will enable us to use [ ] instead of the default < >, in parameter names.
Add this line in the end of configuration() method -

.useNameDelimiterLeft("[").useNameDelimiterRight("]"));



MyStories.java:

public class MyStories extends JUnitStories {

private final static Logger log = Logger.getLogger(MyStories.class);

public MyStories() {

configuredEmbedder().embedderControls().doGenerateViewAfterStories(true).doIgnoreFailureInStories(true)

.doIgnoreFailureInView(true).useThreads(2).useStoryTimeoutInSecs(300);

// Custom Config >> Added to enable meta tag based filtering

List<String> metaFilters = new ArrayList<String>();

metaFilters.add(System.getProperty("metaFilters", "-skip"));

configuredEmbedder().useMetaFilters(metaFilters);

}

@Override

public Configuration configuration() {

log.info("setting JB configurations");

Class<? extends Embeddable> embeddableClass = this.getClass();

// Start from default ParameterConverters instance

ParameterConverters parameterConverters = new ParameterConverters();

// factory to allow parameter conversion and loading from external resources (used by StoryParser too)

ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(new LocalizedKeywords(),
new LoadFromClasspath(embeddableClass), parameterConverters);

// add custom converters

parameterConverters.addConverters(new DateConverter(new SimpleDateFormat("yyyy-MM-dd")),

new ExamplesTableConverter(examplesTableFactory));

return new MostUsefulConfiguration()

.useStoryLoader(new LoadFromClasspath(embeddableClass))

.useStoryParser(new RegexStoryParser(examplesTableFactory))

.useStoryReporterBuilder(new StoryReporterBuilder()

.withCodeLocation(CodeLocations.codeLocationFromClass(embeddableClass))

.withDefaultFormats()

.withFormats(CONSOLE, TXT, HTML, XML))

.useParameterConverters(parameterConverters)

// Custom Config >> This makes jbehave interpret the name contained between the delimiters as the
// parameter name and look it up in the parameters provided by the Examples table

.useParameterControls(new ParameterControls().useDelimiterNamedParameters(true)

// Custom Config >> This would define the delimiters to be used for parameter names. This will
// enable us to use [ ] instead of the default < >, in parameter names

.useNameDelimiterLeft("[").useNameDelimiterRight("]"));

}

@Override

public InjectableStepsFactory stepsFactory() {

return new InstanceStepsFactory(configuration(), new MySteps());

}

@Override

protected List<String> storyPaths() {

return new StoryFinder().findPaths(codeLocationFromClass(this.getClass()), "**/*.story", "**/excluded*.story");

}


}

No comments:

Post a Comment