【问题标题】:Configuration for loading story from String?从字符串加载故事的配置?
【发布时间】:2014-11-02 19:41:51
【问题描述】:

我有一个 Web 服务将 Gherkins 故事发布到后端,并希望将这些故事作为字符串加载到配置中,而不必将它们保存为文件。

这可以实现吗?

【问题讨论】:

    标签: jbehave


    【解决方案1】:

    您需要实现自定义故事加载器,该加载器将从 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 );
        }
    }
    

    从网络服务收集故事并将它们作为具有唯一故事名称和故事主体的地图返回的类:

    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 上的第一个项目,我还在学习如何做 :)

    【讨论】:

    • 我刚刚创建了一个私有内部类,它扩展了 LoadFromClasspath 并覆盖了 loadResourceAsText() 但这绝对是一种正式的方法。 Gist here.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2014-10-09
    • 2022-12-17
    • 2020-06-19
    • 2020-05-27
    • 2019-10-31
    • 2011-06-23
    相关资源
    最近更新 更多