gpt4 book ai didi

java - JBehave 如何在步骤之间保持对象状态

转载 作者:行者123 更新时间:2023-11-29 04:49:24 31 4
gpt4 key购买 nike

我正在使用 JBehave 编写 BDD 集成测试。
问题: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”,同时将值打印到控制台,我可以成功看到它。但是当第二步即执行步骤时,我得到 null 作为实例变量“str”的值。如何让 JBehave 在执行各个步骤后不清除对象的状态?

最佳答案

对于前来寻找此问题答案的任何人,我确实通过 JBehave google group 社区的帮助找到了正确的解决方案。该解决方案非常简单,而不是使用 ScanningStepFactory 使用 InstanceStepsFactory 并且应该保留对象的状态。讨论链接:Google Group discussion

任何来这里回答的人的片段:

package runner;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
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.reporters.CrossReference;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
//import org.jbehave.core.steps.CandidateSteps;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
//import org.jbehave.core.steps.ScanningStepsFactory;
import org.junit.runner.RunWith;

import de.codecentric.jbehave.junit.monitoring.JUnitReportingRunner;
import stepdefs.StepDefs;
//import stepdefs.BarStep;
//import stepdefs.FooStep;

@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 List<CandidateSteps> candidateSteps() {
return new InstanceStepsFactory(configuration(), new FooStep(), new BarStep(), new StepDefs())
.createCandidateSteps();
}*/

@Override
public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration(), new StepDefs());
// return new ScanningStepsFactory(configuration(), "stepdefinitions");
}

@Override
public List<String> storyPaths() {
StoryFinder finder = new StoryFinder();
return finder.findPaths(CodeLocations.codeLocationFromClass(getClass()), Arrays.asList("**/Sample.story"),
null);
}
}

关于java - JBehave 如何在步骤之间保持对象状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35980413/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com