我有一个Web服务将Gherkins故事发布到后端,并希望将这些故事作为字符串加载到配置中,而不必将它们保存为文件。
这可以完成吗?
答案 0 :(得分:1)
您需要实现您的custoom故事加载器,它将从Web服务收集故事并将其传递给JBehave。
您还需要使用GherkinStoryParser将故事从Gherking格式翻译为JBehave格式。
示例配置可能如下例所示。
自定义故事加载程序,用于从字符串映射中检索故事:
public class MyStoryLoader implements StoryLoader {
private Map<String,String> stories;
public MyStoryLoader( Map<String,String> storiesToLoad){
this.stories = storiesToLoad;
}
public String loadStoryAsText(String storyName) {
return stories.get( storyName );
}
}
某些类从Web服务收集故事并将其作为具有唯一故事名称和故事正文的地图返回:
public class StoryCollectorFromWebService {
private final static String storyTemplate = "Feature: A story that is saved in the string\n"
+ "\n"
+ "Scenario: Read the scenario from the string\n"
+ "\n"
+ "Given There is some story named ${name} saved in the string\n"
+ "When I run this story named ${name}\n"
+ "Then I can see it's results";
// This is a method that collects stories from the Web Service and saves them in a map of strings
public Map<String,String> getStoriesFromWebService(){
Map<String,String> storiesFromWebService = new HashMap<String,String>();
String storyNames[] = {"A","B","C","ABC","Some story", "Another story"};
for(String storyName: storyNames)
storiesFromWebService.put( storyName, storyTemplate.replace("${name}", storyName));
return storiesFromWebService;
}
}
以及使用我们的StoryLoader和GherkinStoryParser运行这些故事的示例配置:
public class RunAs_JUnitStories extends JUnitStories {
public RunAs_JUnitStories() {
configuredEmbedder().embedderControls().doGenerateViewAfterStories(true).doIgnoreFailureInStories(true)
.doIgnoreFailureInView(true).useThreads(1).useStoryTimeoutInSecs(60);
}
Map<String,String> storiesFromWebService = new StoryCollectorFromWebService().getStoriesFromWebService();
@Override
protected List<String> storyPaths() {
return new ArrayList<String>( storiesFromWebService.keySet());
}
@Override
public Configuration configuration() {
Class<? extends Embeddable> embeddableClass = this.getClass();
ParameterConverters parameterConverters = new ParameterConverters();
ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(new LocalizedKeywords(), new LoadFromClasspath(embeddableClass), parameterConverters);
parameterConverters.addConverters(new DateConverter(new SimpleDateFormat("yyyy-MM-dd")),
new ExamplesTableConverter(examplesTableFactory));
return new MostUsefulConfiguration()
// Use custom story loader
.useStoryLoader(new MyStoryLoader( storiesFromWebService ))
// Use Gherkin parser
.useStoryParser( new GherkinStoryParser() )
.useStoryReporterBuilder(new StoryReporterBuilder()
.withCodeLocation(CodeLocations.codeLocationFromClass(embeddableClass))
.withDefaultFormats()
.withMultiThreading(true)
.withFormats(CONSOLE, TXT, HTML, XML))
.useParameterConverters(parameterConverters);
}
@Override
public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration(), new MySteps());
}
}
以下是工作示例的链接:https://github.com/kordirko/TestJBB
你可以尝试将它导入Eclipse并使用它,但是如果出现问题我会道歉
这是我在github上的第一个项目,我还在学习如何做到这一点:)