我正在使用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