Skip to content

Instantly share code, notes, and snippets.

@mallim
Created October 9, 2013 01:47
Show Gist options
  • Save mallim/6894875 to your computer and use it in GitHub Desktop.
Save mallim/6894875 to your computer and use it in GitHub Desktop.
JBehave with Spring Framework Setup - Advantage of this approach - can control the story sequence
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.configuration.spring.SpringStoryControls;
import org.jbehave.core.failures.FailingUponPendingStep;
import org.jbehave.core.failures.PendingStepStrategy;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.parsers.RegexPrefixCapturingPatternParser;
import org.jbehave.core.reporters.CrossReference;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.ParameterConverters;
import org.jbehave.core.steps.SilentStepMonitor;
import org.jbehave.core.steps.StepMonitor;
import org.jbehave.core.steps.spring.SpringApplicationContextFactory;
import org.jbehave.core.steps.spring.SpringStepsFactory;
import org.springframework.context.ApplicationContext;
import java.util.ArrayList;
import java.util.List;
import static org.jbehave.core.io.CodeLocations.codeLocationFromPath;
import static org.jbehave.core.reporters.Format.*;
/**
* Use for running of JBehave test cases during development
*
* Take note that Jenkins should not be running this class
*/
public class TestStandaloneStories extends JUnitStories{
private ApplicationContext context;
ParameterConverters parameterConverters = new ParameterConverters();
PendingStepStrategy pendingStepStrategy = new FailingUponPendingStep();
CrossReference crossReference = new CrossReference()
.withJsonOnly()
.withPendingStepStrategy(pendingStepStrategy)
.withOutputAfterEachStory(true)
.excludingStoriesWithNoExecutedScenarios(true);
Format[] formats = new Format[] { CONSOLE, TXT, HTML, XML };
StepMonitor stepMonitor = new SilentStepMonitor();
@Override
public Configuration configuration() {
StoryReporterBuilder reporterBuilder = new StoryReporterBuilder()
.withCodeLocation(CodeLocations.codeLocationFromClass(this.getClass()))
.withDefaultFormats()
.withFailureTrace(true)
.withFailureTraceCompression(true)
.withDefaultFormats()
.withFormats(formats)
.withCrossReference(crossReference);
Configuration configuration = new MostUsefulConfiguration()
.useFailureStrategy( new FailingUponPendingStep() )
.useStepPatternParser( new RegexPrefixCapturingPatternParser( "$" ) )
.useStoryControls( new SpringStoryControls().doResetStateBeforeScenario( false ) )
.useStoryLoader( new LoadFromClasspath( this.getClass() ) )
.useStoryReporterBuilder( reporterBuilder )
.useParameterConverters( parameterConverters )
.useStepMonitor( stepMonitor );
return configuration;
}
@Override
public InjectableStepsFactory stepsFactory() {
this.context = new SpringApplicationContextFactory("classpath:TestStandaloneStories-context").createApplicationContext();
return new SpringStepsFactory(configuration(), this.context);
}
/**
* The stories to be exeucted is being configured in the spring context
*
* <pre>
* {@code
* <util:list id="storyPaths" value-type="java.lang.String">
* <!-- the stories are listed here -->
* </util:list>
* }
* </pre>
*/
@Override
protected List<String> storyPaths() {
List<String> storyPaths1 = (List<String>) this.context.getBean( "storyPaths" );
List<String> allPaths = new ArrayList<String>();
for( String aPath: storyPaths1 )
{
List<String> storyPaths = new StoryFinder().findPaths( codeLocationFromPath( "{{YOUR_ROOT_PATH_TO_STORIES}}" ), aPath, "" );
allPaths.addAll( storyPaths );
}
return allPaths;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3..xsd">
<util:list id="storyPaths" value-type="java.lang.String">
<!--
<value>stories/1.story</value>
<value>stories/another_dir/*.story</value>
</util:list>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment