我正在使用JBehave编写BDD集成测试。
问题:JBehave清除对象(实例变量)的状态,同时执行各个步骤
代码: StepDefinition:JBehave如何在步骤之间保持对象状态
public class StepDefs {
private String str;
@Given("step represents a precondition to an $event")
public void given(String event){
str=event;
System.out.println("Given: "+str);
}
@When("step represents the occurrence of the event")
public void when() {
System.out.println("When: "+str);
}
@Then("step represents the outcome of the event")
public void then() {
}
}
故事:
Sample story
Narrative:
In order to communicate effectively to the business some functionality
As a development team
I want to use Behaviour-Driven Development
Scenario: A scenario is a collection of executable steps of different type
Given step represents a precondition to an event
When step represents the occurrence of the event
Then step represents the outcome of the event
JBehaveJUnitTestRunner:
@RunWith(JUnitReportingRunner.class)
public class JBehaveTestsRunner extends JUnitStories {
private CrossReference xref = new CrossReference();
public JBehaveTestsRunner() {
configuredEmbedder().embedderControls().doGenerateViewAfterStories(true).doIgnoreFailureInStories(true)
.doIgnoreFailureInView(true).doVerboseFailures(true);// .useThreads(1);
}
@Override
public Configuration configuration() {
Properties viewResources = new Properties();
viewResources.put("decorateNonHtml", "true");
return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(this.getClass().getClassLoader()))
.useStoryReporterBuilder(
new StoryReporterBuilder().withFormats(Format.HTML, Format.CONSOLE, Format.STATS)
.withViewResources(viewResources).withFailureTrace(true).withFailureTraceCompression(false)
.withCrossReference(xref));
}
@Override
public InjectableStepsFactory stepsFactory() {
return new ScanningStepsFactory(configuration(), "stepdefs");
}
@Override
public List<String> storyPaths() {
StoryFinder finder = new StoryFinder();
return finder.findPaths(CodeLocations.codeLocationFromClass(getClass()), Arrays.asList("**/Simple.story"), null);
}
}
实际输出:
Processing system properties {}
Using controls EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=true,ignoreFailureInView=true,verboseFailures=true,verboseFiltering=false,storyTimeouts=300,threads=1,failOnStoryTimeout=false]
(BeforeStories)
Running story stories/Simple.story
Sample story
(stories/Simple.story)
Narrative:
In order to communicate effectively to the business some functionality
As a development team
I want to use Behaviour-Driven Development
Scenario: A scenario is a collection of executable steps of different type
**Given: event**
Given step represents a precondition to an event
**When: null**
When step represents the occurrence of the event
Then step represents the outcome of the event
(AfterStories)
Generating reports view to 'C:\WORKING\lunaworkspace\pkeautomation\target\jbehave' using formats '[html, console, stats, junitscenarioreporter]' and view properties '{decorateNonHtml=true}'
log4j:WARN No appenders could be found for logger (freemarker.cache).
log4j:WARN Please initialize the log4j system properly.
Reports view generated with 3 stories (of which 1 pending) containing 2 scenarios (of which 1 pending)
可以看出在输出:在给定的步骤我接受对此我它初始化为实例变量“STR”字符串参数,同时进行打印的值到控制台我可以成功地看到它。但是,当第二步即执行步骤时,我得到空值作为实例变量“str”的值。如何让JBehave在执行各个步骤后不清除对象的状态?
的可能的复制[JBehave保持整个给定的步骤数据/时/然后在一个场景(http://stackoverflow.com/questions/9389845/jbehave-maintain-data-across-steps-given-when-then-during-a-scenario) - 尽管我只是看了看,并没有真正解释如何做到这一点,所以收回近距离投票。链接到答案而不把这些答案的实际内容放下来是不好的,人们! – Lunivore